Skip to content

Instantly share code, notes, and snippets.

@xnorcode
Created May 3, 2018 17:01
Show Gist options
  • Save xnorcode/e0fb10a15074493b71b478f59ea3d03a to your computer and use it in GitHub Desktop.
Save xnorcode/e0fb10a15074493b71b478f59ea3d03a to your computer and use it in GitHub Desktop.
Linked List Remove Node
public class LinkedList<T> {
// Reference to the head node
Node head;
public void removeNode(T data){
// check if list empty
if(head == null) return;
// check head's data if equal
// and assign next node as head
if(head.data == data){
head = head.next;
return;
}
// get head reference
Node current = head;
// iterate list
while(current.next != null){
// check next node's data
// skip next node
if(current.next.data == data){
current.next = current.next.next;
return;
}
// go to next node
current = current.next;
}
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment