Advertisement
Aldin_SXR

addToRear()

Feb 27th, 2024
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.63 KB | None | 0 0
  1. /* Add a new item to the end of the list */
  2. public void addToRear(Data data) {
  3.     Node<Data> newNode = new Node<>();          // 1
  4.     newNode.data = data;                        // 1
  5.  
  6.     if (head == null) {                         // 2
  7.         head = newNode;                         // 2
  8.     } else {
  9.         Node<Data> current = head;              // 3
  10.         while (current.next != null) {          // 4
  11.             current = current.next;             // 4
  12.         }                                       // 4
  13.         current.next = newNode;                 // 5
  14.     }
  15.     size++;                                     // 6
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement