Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. class Node {
  2. constructor(data, next) {
  3. this.data = data;
  4. this.next = next;
  5. }
  6. }
  7.  
  8. function createListFromArray(array) {
  9. let list = null;
  10. for (const item of array) {
  11. list = new Node(item, list);
  12. }
  13. return list;
  14. }
  15.  
  16. function displayList(list) {
  17. while (list) {
  18. console.log(list.data);
  19. list = list.next;
  20. }
  21. }
  22.  
  23. function length(head) {
  24. let length = 0;
  25.  
  26. while (head != null) {
  27. length++;
  28. head = head.next;
  29. }
  30.  
  31. return length;
  32. }
  33.  
  34. let array = [3,2,1];
  35. let list = createListFromArray(array);
  36. console.log(length(list));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement