Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. class LinkedListNode {
  2. constructor(value, next = null) {
  3. this.value = value;
  4. this.next = next;
  5. }
  6. }
  7.  
  8. class LinkedList {
  9. constructor() {
  10. this.head = null;
  11. this.tail = null;
  12. this.el = [];
  13. }
  14.  
  15. /**
  16. * Insert a new node to the head of linked list
  17. *
  18. * @param value Value of the new node
  19. * @returns {LinkedList}
  20. */
  21. prepend = function prepend(value) {
  22.  
  23. let newNode = new LinkedListNode(value, this.head);
  24. this.head = newNode;
  25. console.log(this.head);
  26.  
  27. if (!this.tail) {
  28. this.tail = newNode;
  29. }
  30.  
  31. this.el.push(newNode);
  32.  
  33. return this;
  34. };
  35.  
  36. /**
  37. * Insert a new node to the tail of linked list
  38. *
  39. * @param value Value of the new node
  40. * @returns {LinkedList}
  41. */
  42. append = function append(value) {
  43.  
  44. let newNode = new LinkedListNode(value);
  45.  
  46. // If the linked list is empty
  47. if (!this.head) {
  48. this.head = newNode;
  49. this.el.push(newNode);
  50. this.tail = newNode;
  51.  
  52. return this;
  53. }
  54.  
  55. this.el.push(newNode);
  56. this.tail.next = newNode;
  57. this.tail = newNode;
  58. };
  59.  
  60. /**
  61. * Delete a node at a given position
  62. *
  63. * @param position The position of the node to delete
  64. * @returns {null|*}
  65. */
  66. remove = function remove(position) {
  67.  
  68. let current;
  69.  
  70. // If the linked list is empty
  71. if (!this.head || position < 1) {
  72. return null;
  73. }
  74.  
  75. // If we are deleting the head
  76. if (position === 1) {
  77. this.head = this.head.next;
  78. this.el.pop();
  79. return null;
  80. }
  81.  
  82. current = this.head;
  83.  
  84. // Traverse the linked list
  85. for (let i = 0; i < position - 2; i++) {
  86. current = current.next;
  87. }
  88.  
  89. let target = current.next;
  90. current.next = target.next;
  91.  
  92. // Empty the current
  93. return target;
  94. };
  95.  
  96. /**
  97. * Reverse the linked list
  98. */
  99. reverse = function reverse() {
  100.  
  101. let prevNode = null;
  102. let currentNode = this.head;
  103. let nextNode;
  104.  
  105. while (currentNode) {
  106. // Store the next node into a variable
  107. nextNode = currentNode.next;
  108. // Change current link to the previous node
  109. currentNode.next = prevNode;
  110. // Move previous and current next to one position
  111. prevNode = currentNode;
  112. currentNode = nextNode;
  113. }
  114.  
  115. this.tail = this.head;
  116. this.head = prevNode;
  117. };
  118.  
  119. /**
  120. * Recursively reverse the linked list
  121. * @param headNode The head of the the linked list
  122. * @returns {*}
  123. */
  124.  
  125. recursiveReverse = function recursiveReverse(headNode) {
  126.  
  127. if (headNode === null || headNode.next === null) {
  128. return;
  129. }
  130.  
  131. let recurReverse = this.recursiveReverse(headNode.next);
  132. headNode.next.next = headNode;
  133. headNode.next = null;
  134.  
  135. return recurReverse;
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement