Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. class Node{
  2. constructor(data, nuxt =null){
  3. this.data = data;
  4. this.next = null;
  5. }
  6. }
  7.  
  8. class LinkedList{
  9. constructor(){
  10. this.head = null;
  11. }
  12. //insertFirst
  13. insertFirst(data){
  14. this.head = new Node(data);
  15. }
  16. //insertLast
  17. insertLast(data){
  18. if(this.head === null){
  19. this.head = new Node(data);
  20. } else {
  21. let node = this.head;
  22. while(node.next){
  23. node = node.next;
  24. }
  25. node.next = new Node(data);
  26. }
  27. }
  28.  
  29. //size
  30. size(){
  31. let node = this.head;
  32. let counter = 0;
  33. while(node){
  34. node = node.next;
  35. counter++;
  36. }
  37. return counter;
  38. }
  39. //getFirst
  40. getFirst(){
  41. return this.head;
  42. }
  43. //getLast
  44. getLast(){
  45. let node = this.head;
  46. while(node){
  47. if(!node.next){
  48. return node;
  49. }
  50. node = node.next;
  51. }
  52. }
  53. //insertFirst
  54. insertFirst(item){
  55. this.head = new _Node(item, this.head);
  56. }
  57. //getAt
  58. getAt(index){
  59. let node = this.head;
  60. let counter = 0;
  61. while(node){
  62. if(index === counter){
  63. return node;
  64. }
  65. node = node.next;
  66. counter++;
  67. }
  68. }
  69. //removeAt
  70. removeAt(index){
  71. let previous = this.getAt(index-1);
  72. previous.next = previous.next.next;
  73. }
  74. //insertAt
  75. insertAt(index, data){
  76. let previous = this.getAt(index-1);
  77. const node = new Node(data, previous.next);
  78. previous.next = new Node(data);
  79. }
  80. //reverse
  81. reverse(){
  82. let curr = this.head;
  83. let previous = null;
  84. while(curr){
  85. let next = curr.next;
  86. curr.next = previous;
  87. previous = curr;
  88. curr = next;
  89. }
  90. return previous;
  91. }
  92.  
  93. insertAfter(inList, insertVal){
  94. let node = this.head;
  95. while(node){
  96. if(node.data === inList){
  97. node.next = new Node(insertVal);
  98. }
  99. node = node.next;
  100. }
  101. }
  102.  
  103. insertBefore(inList, insertVal){
  104. if(this.head.data === inList){
  105. this.head = new Node(insertVal);
  106. }
  107. let previous = this.head;
  108. let curr = this.head.next;
  109. while(curr){
  110. // console.log('this is curr', curr);
  111. if(curr.data === inList){
  112. console.log('this is firing');
  113. previous.next = new Node(insertVal);
  114. }
  115. previous = curr;
  116. curr = curr.next;
  117. }
  118. }
  119. thirdFromEnd(){
  120. let countTo = this.size()-3;
  121. console.log('this is countTo', countTo);
  122. let counter = 0;
  123. let node = this.head;
  124. while(node.nnext){
  125. if(counter === countTo){
  126. return node;
  127. }
  128. node = node.next;
  129. counter++;
  130. }
  131. }
  132.  
  133. Middle(){
  134. let fast = this.head;
  135. let slow = this.head;
  136. while(fast.next && fast.next.next){
  137. slow = slow.next;
  138. fast = fast.next.next;
  139. }
  140. return slow;
  141. }
  142.  
  143. Cycle(){
  144. let fast = this.head;
  145. let slow = this.head;
  146. while(fast.next && fast.next.next){
  147. if(slow === fast){
  148. return true;
  149. }
  150. slow = slow.next;
  151. fast= fast.next.next;
  152. }
  153. return false;
  154. }
  155.  
  156.  
  157. }//class end
  158. let pets = new LinkedList();
  159. pets.insertLast('kitten');
  160. pets.insertLast('puppy');
  161. pets.insertLast('parrot');
  162. pets.insertLast('hamster');
  163. pets.insertLast('farrett');
  164. console.log(pets.Middle());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement