Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Linked List Add Node To Tail
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