Skip to content

Instantly share code, notes, and snippets.

@xnorcode
Created May 3, 2018 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xnorcode/9c4c3e1feaa0fbadf9755d9c309dbd98 to your computer and use it in GitHub Desktop.
Save xnorcode/9c4c3e1feaa0fbadf9755d9c309dbd98 to your computer and use it in GitHub Desktop.
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