Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions linked-list/LinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
class LinkedList {
private static ListNode head = null;
private static ListNode tail = null;
LinkedList(int data) {
ListNode node = new ListNode(data);
head = node;
tail = node;
}

public void printList(){
System.out.print("START -> ");
ListNode ptr = head;
while(ptr != null){
System.out.print(ptr.getData()+" -> ");
ptr = ptr.getNext();
}
System.out.println("END");
}

public void insertHead(int data){
ListNode node = new ListNode(data);
if(head == null)
head = tail = node;
else{
node.setNext(head);
head = node;
}
}

public void insertTail(int data){
ListNode node = new ListNode(data);
if(head == null)
head = tail = node;
else{
tail.setNext(node);
tail = node;
}
}

public void insertAfterNodeData(int afterThisData, int data){
ListNode node = new ListNode(data);
if(head == null)
head = tail = node;
else{
ListNode ptr = head;
while(ptr.getData() != afterThisData)
ptr = ptr.getNext();
node.setNext(ptr.getNext());
ptr.setNext(node);
}
}

public void deleteHead(){
if(head == null){
System.out.print("List Empty !");
return;
}
else{
ListNode ptr = head;
head = ptr.getNext();
ptr = null;
}
}

public void deleteTail(){
if(head == null){
System.out.print("List Empty !");
return;
}
else{
ListNode ptr = head;
while(ptr.getNext().getNext() != null)
ptr = ptr.getNext();
ptr.setNext(null);
tail = ptr;
tail.setNext(null);
}
}

public void deleteNodeWithData(int toBeDeleted){
if(head == null){
System.out.print("List Empty !");
return;
}
else{
ListNode ptr = head;
while(ptr.getNext().getData() != toBeDeleted)
ptr = ptr.getNext();
ptr.setNext(ptr.getNext().getNext());
ptr = ptr.getNext();
ptr = null;
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedList L = new LinkedList(21);
L.insertHead(15);
L.insertTail(36);
L.insertTail(50);
L.insertTail(45);
L.insertAfterNodeData(36, 10);
L.printList();
L.deleteNodeWithData(10);
L.printList();
}
}
14 changes: 14 additions & 0 deletions linked-list/ListNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class ListNode{
private int data;
private ListNode next = null;

ListNode(int data){
this.data = data;
}

public int getData(){ return this.data; }
public ListNode getNext(){ return this.next; }

public void setData(int data){ this.data = data; }
public void setNext(ListNode next){ this.next = next; }
}
10 changes: 10 additions & 0 deletions linked-list/info
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Java files for singly Linked List

I have hardcoded the insertion and deletion operations, instead of giving the user choice feature.
The main aim of this repository is the core algorithm, not user interaction.
If needed, switch case or other similar structures can be used to let the user decide what methods to call at what time.

Also, there is only singly LL here.
Implementing doubly LL and circular LL is very much similar once you understand singly LL.
Maybe later I will add them too.
Feel free to ask me for them in case I don't.