Created
May 3, 2018 15:37
-
-
Save xnorcode/9c4c3e1feaa0fbadf9755d9c309dbd98 to your computer and use it in GitHub Desktop.
Linked List Add Node To Tail
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LinkedList<T> { | |
// Reference to the head node | |
Node head; | |
public void addToTail(T data){ | |
// create new node | |
Node newNode = new Node(data, null); | |
// Check if head is null and set new node as head | |
if(head == null){ | |
head = newNode; | |
return; | |
} | |
// get head reference | |
Node current = head; | |
// iterate until the last node | |
while(current.next != null){ | |
current = current.next; | |
} | |
// add new node to the tail | |
current.next = newNode; | |
} | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment