Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. class Node{
  2. constructor(data, next = null){
  3. this.data = data,
  4. this.next = next
  5. }
  6. }
  7.  
  8. class LinkedList{
  9. constructor(){
  10. this.head = null;
  11. }
  12. }
  13.  
  14. // A list object is created with a property head, currently pointing at null
  15.  
  16. let list = new LinkedList();
  17.  
  18. LinkedList.prototype.insertAtBeginning = function(data){
  19. // A newNode object is created with property data and next = null
  20. let newNode = new Node(data);
  21. // The pointer next is assigned head pointer so that both pointers now point at the same node.
  22. newNode.next = this.head;
  23. // As we are inserting at the beginning the head pointer needs to now point at the newNode.
  24.  
  25. this.head = newNode;
  26. return this.head;
  27. }
  28.  
  29. LinkedList.prototype.insertAtEnd = function(data){
  30. // A newNode object is created with property data and next=null
  31.  
  32. let newNode = new Node(data);
  33. // When head = null i.e. the list is empty, then head itself will point to the newNode.
  34. if(!this.head){
  35. this.head = newNode;
  36. return this.head;
  37. }
  38. // Else, traverse the list to find the tail (the tail node will initially be pointing at null), and update the tail's next pointer.
  39. let tail = this.head;
  40. while(tail.next !== null){
  41. tail = tail.next;
  42. }
  43. tail.next = newNode;
  44. return this.head;
  45. }
  46. // A helper function getAt() is defined to get to the desired position. This function can also be later used for performing delete operation from a given position.
  47. LinkedList.prototype.getAt = function(index){
  48. let counter = 0;
  49. let node = this.head;
  50. while (node) {
  51. if (counter === index) {
  52. return node;
  53. }
  54. counter++;
  55. node = node.next;
  56. }
  57. return null;
  58. }
  59. // The insertAt() function contains the steps to insert a node at a given index.
  60. LinkedList.prototype.insertAt = function(data, index){
  61. // if the list is empty i.e. head = null
  62. if (!this.head) {
  63. this.head = new Node(data);
  64. return;
  65. }
  66. // if new node needs to be inserted at the front of the list i.e. before the head.
  67. if (index === 0) {
  68. this.head = new Node(data, this.head);
  69. return;
  70. }
  71. // else, use getAt() to find the previous node.
  72. const previous = this.getAt(index - 1);
  73. let newNode = new Node(data);
  74. newNode.next = previous.next;
  75. previous.next = newNode;
  76.  
  77. return this.head
  78. }
  79.  
  80. LinkedList.prototype.deleteFirstNode = function(){
  81. if(!this.head){
  82. return;
  83. }
  84. this.head = this.head.next;
  85. return this.head;
  86. }
  87.  
  88. LinkedList.prototype.deleteLastNode = function(){
  89. if(!this.head){
  90. return null;
  91. }
  92. // if only one node in the list
  93. if(!this.head.next){
  94. this.head = null;
  95. return;
  96. }
  97. let previous = this.head;
  98. let tail = this.head.next;
  99.  
  100. while(tail.next !== null){
  101. previous = tail;
  102. tail = tail.next;
  103. }
  104.  
  105. previous.next = null;
  106. return this.head;
  107. }
  108.  
  109. LinkedList.prototype.deleteAt = function(index){
  110. // when list is empty i.e. head = null
  111. if (!this.head) {
  112. this.head = new Node(data);
  113. return;
  114. }
  115. // node needs to be deleted from the front of the list i.e. before the head.
  116. if (index === 0) {
  117. this.head = this.head.next;
  118. return;
  119. }
  120. // else, use getAt() to find the previous node.
  121. const previous = this.getAt(index - 1);
  122.  
  123. if (!previous || !previous.next) {
  124. return;
  125. }
  126.  
  127. previous.next = previous.next.next;
  128. return this.head
  129. }
  130.  
  131. LinkedList.prototype.deleteList = function(){
  132. this.head = null;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement