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;
}
}
@YassineKADER
Copy link

thank you friend 👍

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