Skip to content

Instantly share code, notes, and snippets.

@akshaynagpal
Last active November 5, 2022 09:16
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save akshaynagpal/14ee8b54cd018bd05b2a3c2432c818dc to your computer and use it in GitHub Desktop.
Save akshaynagpal/14ee8b54cd018bd05b2a3c2432c818dc to your computer and use it in GitHub Desktop.
Linked List From Scratch in Java
package src.LinkedList;
public class LinkedList {
public Node head;
public int listCount;
public LinkedList(){
head = new Node(0);
listCount = 0;
}
public void show(){
Node current = head;
while(current.next!=null){
System.out.print(current.data+" -> ");
current = current.next;
}
System.out.println(current.data);
}
public boolean add(int d){
Node end = new Node(d);
Node current = head;
while(current.next != null){
current = current.next;
}
current.next = end;
listCount++;
System.out.println(d+" appended to tail!");
return true;
}
public boolean add(int d,int index){
Node end = new Node(d);
Node current = head;
int jump;
if(index>listCount || index<1){
System.out.println("Add Failed: index out of bounds of size of linked list!!");
return false;
}
else{
jump = 0;
while(jump<index-1){
current = current.next;
jump++;
}
end.next = current.next;
current.next = end;
listCount++;
System.out.println("Success! "+d+" added at index "+index);
return true;
}
}
public boolean deleteNodeWithData(int d){
Node current = head;
while(current.next!=null){
if(current.next.data==d){
current.next = current.next.next;
listCount--;
System.out.println("Success! Node with data "+d+" deleted.");
return true;
}
current = current.next;
}
System.out.println("Delete Failed: No node found with given data!");
return false;
}
public boolean deleteNodeAtIndex(int index){
Node current = head;
int jump;
if(index>listCount || index<1){
System.out.println("Delete Failed: index out of bounds of size of linked list!!");
return false;
}
else{
jump=0;
while(jump<index-1){
current = current.next;
jump++;
}
current.next = current.next.next;
System.out.println("Success! Node at index "+index+" deleted.");
listCount--;
return true;
}
}
public static void main(String[] args) {
LinkedList L = new LinkedList();
L.add(1);
L.show();
L.add(2);
L.show();
L.add(3);
L.show();
L.deleteNodeWithData(2);
L.show();
L.deleteNodeAtIndex(3);
L.show();
L.deleteNodeAtIndex(1);
L.show();
/*
< OUTPUT >
1 appended to tail!
0 -> 1
2 appended to tail!
0 -> 1 -> 2
3 appended to tail!
0 -> 1 -> 2 -> 3
Success! Node with data 2 deleted.
0 -> 1 -> 3
Delete Failed: index out of bounds of size of linked list!!
0 -> 1 -> 3
Success! Node at index 1 deleted.
0 -> 3
<end of OUTPUT>
*/
}
}
package src.LinkedList;
public class Node {
Node next = null;
int data;
public Node(int d){
data = d;
}
}
@akshaynagpal
Copy link
Author

I searched the internet for a LinkedList implementation from scratch in Java that could be understood by a beginner (like me). Didn't find anything useful so did it on my own!

@ProtoGanesh
Copy link

Good job bro. many thank you. I've signed up to just comment .keep up.

@Soumik007
Copy link

Good work man

@akshaynagpal
Copy link
Author

Thanks @Soumik007 and @SGMMYLOVE .

@juankg214
Copy link

juankg214 commented Aug 19, 2017

One comment: in deleteNodeWithData(int d), if the data d is on the head node, the node won't be deleted. And also if more than one node contains the data, only the first one will be deleted. This is how i fixed it:

`

public void DeleteWithData(T data){
Node current = head;

while(current.next != null){
if(current.next.data==data){
     current.next=current.next.next;
     size--;
}
current=current.next;
}
if(head.data==data){
    head=head.next;
}
}

`

@fractalwizz
Copy link

Similarly to what juankg stated, I found a small but noticable issue with deleteNodeWithData. The check for data equivalence only occurs when the current node.next != null. What if the node you're looking for is the last element? This while loop would skip over it, and return false.

Otherwise, good stuff.

@RaiyanReza
Copy link

Could someone please explain how line 60 and all 61 works, I don't under how the dot operator works and what it does in these two scenarios.

@sandeepnegi1996
Copy link

Thank you so much for this it really helped me

@jAndrewtomich
Copy link

Nice job, my friend!

@akshaynagpal
Copy link
Author

Similarly to what juankg stated, I found a small but noticable issue with deleteNodeWithData. The check for data equivalence only occurs when the current node.next != null. What if the node you're looking for is the last element? This while loop would skip over it, and return false.

Otherwise, good stuff.

Thanks for the feedback. However, notice that we're doing

....
while(current.next!=null){
            if(current.next.data==d){
....

so if the last node has data d it will be deleted when pointer is at 2nd last node.

Otherwise, @juankg 's comment makes sense, I will edit the code in my free time.

@YassineKADER
Copy link

thank you friend 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment