Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. class Node {
  2. constructor (val) {
  3. this.val = val;
  4. this.next = null;
  5. }
  6. }
  7.  
  8. class SinglyLinkedList {
  9. constructor () {
  10. this.head = null;
  11. this.tail = null;
  12. this.length = 0;
  13. }
  14.  
  15. push (val) {
  16. const node = new Node(val);
  17. if (!this.head) {
  18. this.head = node;
  19. this.tail = node;
  20. } else {
  21. this.tail.next = node;
  22. this.tail = node;
  23. }
  24. this.length ++;
  25. return this;
  26. }
  27.  
  28. pop () {
  29. if (!this.head) {
  30. return undefined;
  31. }
  32.  
  33. let current = this.head;
  34. let newTail = current;
  35.  
  36. while (current.next) {
  37. newTail = current;
  38. current = current.next;
  39. }
  40.  
  41. this.tail = newTail;
  42. this.tail.next = null;
  43. this.length --;
  44.  
  45. if (!this.length) {
  46. this.head = null;
  47. this.tail = null;
  48. }
  49.  
  50. return current;
  51. }
  52.  
  53. shift () {
  54. if (!this.head) {
  55. return undefined;
  56. }
  57.  
  58. const oldHead = this.head;
  59. this.head = oldHead.next;
  60. this.length --;
  61.  
  62. if (!this.length) {
  63. this.head = null;
  64. this.tail = null;
  65. }
  66.  
  67. return oldHead;
  68. }
  69.  
  70. unshift (val) {
  71. const node = new Node(val);
  72. if (!this.head) {
  73. this.head = node;
  74. this.tail = node;
  75. } else {
  76. node.next = this.head;
  77. this.head = node;
  78. }
  79. this.length ++;
  80.  
  81. return this;
  82. }
  83.  
  84. // O(N)
  85. get (idx) {
  86. if (idx < 0 || idx >= this.length) {
  87. return null;
  88. }
  89.  
  90. let node = this.head;
  91.  
  92. for (let i = 0; i < idx; i ++) {
  93. node = node.next;
  94. }
  95.  
  96. return node;
  97. }
  98.  
  99. set (idx, val) {
  100. const node = this.get(idx);
  101. if (node) {
  102. node.val = val;
  103. return true;
  104. }
  105. return false;
  106. }
  107.  
  108. // O(1)
  109. insert (idx, val) {
  110. if (idx < 0 || idx > this.length) {
  111. return false;
  112. }
  113.  
  114. if (idx === 0) {
  115. return !!this.unshift(val);
  116. }
  117.  
  118. if (idx === this.length) {
  119. return !!this.push(val);
  120. }
  121.  
  122. const previous = this.get(idx - 1);
  123. const newNode = new Node(val);
  124. const temp = previous.next;
  125. previous.next = newNode;
  126. newNode.next = temp;
  127. this.length ++;
  128.  
  129. return true;
  130. }
  131.  
  132. // O(1) at the beginning or O(N)
  133. remove (idx) {
  134. if (idx < 0 || idx >= this.length) {
  135. return undefined;
  136. }
  137.  
  138. if (idx === 0) {
  139. return this.shift();
  140. }
  141.  
  142. if (idx === this.length - 1) {
  143. return this.pop();
  144. }
  145.  
  146. const previous = this.get(idx - 1);
  147. const removed = previous.next;
  148. previous.next = removed.next;
  149. this.length --;
  150.  
  151. return removed;
  152. }
  153.  
  154. reverse () {
  155. if (this.length === 0 || this.length === 1) {
  156. return this;
  157. }
  158.  
  159. let current = this.head;
  160. this.head = this.tail;
  161. this.tail = current;
  162. let previous = null;
  163. let next = null;
  164.  
  165. for (let i = 0; i < this.length; i++) {
  166. next = current.next;
  167. current.next = previous;
  168. previous = current;
  169. current = next;
  170. }
  171.  
  172. return this;
  173. }
  174. }
  175.  
  176. const list = new SinglyLinkedList();
  177.  
  178. list.push('5');
  179. list.push('55');
  180. list.push('555');
  181. list.push('5555');
  182. list.reverse();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement