Skip to content

This repository includes all you need to know about a Linked List

Notifications You must be signed in to change notification settings

anchitctrl/Linked-List

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Linked-List 👨‍💻

Namaste 🙏 , first of all welcome. If you are looking for an amazing resource to learn Linked List, this is the place. I am including all the basics to important questions here which will definitely help you land to your dream object.

Data Structures and Algorithm is the most important topic to give you a Job, and Linked List is something which everyone finds quite easier to learn, so if you think you are good in Linked List you can go through, and revise this topic and keep it fresh.


Zindagi Gulzar honi chahiye, Sort to Linked List bhi hoti hai - Unknown

Table of Contents

Implementation of a Linked List

public class SinglyLinkedList {
	
	private ListNode head;
	
	private static class ListNode {
		private int data; // Can be a generic type
		private ListNode next; // Reference to next ListNode in list
		
		public ListNode(int data) {
			this.data = data;
			this.next = null;
		}
	}
}

Create a Linked List

public static void main(String[] args) {
		SinglyLinkedList sll = new SinglyLinkedList();
		sll.head = new ListNode(10);
		ListNode second = new ListNode(1);
		ListNode third = new ListNode(8);
		ListNode fourth = new ListNode(11);
		
		// Now we will connect them together to form a chain
		sll.head.next = second; // 10 --> 1
		second.next = third; // 10 --> 1 --> 8
		third.next = fourth; // 10 --> 1 --> 8 --> 11 --> null
	}

Print Elements of a Linked List

public void display() {
		ListNode current = head;
		while(current != null) {
			System.out.print(current.data + " --> ");
			current = current.next;
		}
		System.out.print("null");
	}

Length of a Linked List

public int length() {
		if(head == null) {
			return 0;
		}
		int count = 0;
		ListNode current = head;
		while(current != null) {
			count++;
			current = current.next;
		}
		return count;
	}

Insert a node at the Beginning of a Linked List

public void insertFirst(int value) {
		ListNode newNode = new ListNode(value);
		newNode.next = head;
		head = newNode;
	}

Insert a node at the End of a Linked List

public void insertLast(int value) {
		ListNode newNode = new ListNode(value);
		if (head == null) {
			head = newNode;
			return;
		}
		ListNode current = head;
		while (current.next != null) {
			current = current.next;
		}
		current.next = newNode;
	}

Insert a node at the Nth Position of a Linked List

public void insertAtPosition(int data, int position) {
	    ListNode newNode = new ListNode(data);
	    if(position == 0){
	        newNode.next = head;
	        head = newNode;
	    } else {
	        ListNode previous = head;
	        int count = 0;
	        while(count < position - 1){
	            previous = previous.next;
	            count++;
	        }
	        
	        ListNode current = previous.next;
	        newNode.next = current;
	        previous.next = newNode;
	    }
	}

Delete first node from a Linked List

public ListNode deleteFirst() {
        if(head == null) {
            return null;
        }
        ListNode temp = head;
        head = head.next;
        temp.next = null;
        return temp;
    }

Delete End node from a Linked List

public ListNode deleteLast() {
	if(head == null || head.next == null) {
            return head;
	}
	ListNode current = head;
	ListNode previous = null;
		
	while(current.next != null) {
	    previous = current;
	    current = current.next;
	}
	previous.next = null; // break the chain
	return current;	
    }

Delete node at the Nth position of a Linked List

public void deleteAtPosition(int position) {
	    if(position == 0){
	        ListNode temp = head;
	        head = head.next;
	        temp.next = null;
	    } else {
	        ListNode previous = head;
	        int count = 0;
	        while(count < position - 1){
	            previous = previous.next;
	            count++;
	        }
	        
	        ListNode current = previous.next;
	        previous.next = current.next;
	        current.next = null;
	    }
	}

Search a given key or element in a Linked List

public boolean find(int searchKey) {
		if(head == null) {
			return false;
		}
		
		ListNode current = head;
		while(current != null) {
			if(current.data == searchKey) {
				return true;
			}
			current = current.next;
		}
		return false;
	}

Reverse a singly linked list

public ListNode reverse() {
		if(head == null) {
			return head;
		}
		
		ListNode current = head;
		ListNode previous = null;
		ListNode next = null;
		
		while(current != null) {
			next = current.next;
			current.next = previous;
			previous = current;
			current = next;
		}
		return previous;
	}

Find the Middle node in a Linked List

public ListNode getMiddleNode() {
		if(head == null) {
			return null;
		}
		ListNode slowPtr = head;
		ListNode fastPtr = head;
		
		while(fastPtr != null && fastPtr.next != null) {
			slowPtr = slowPtr.next;
			fastPtr = fastPtr.next.next;
		}
		return slowPtr;
	}

Nth node from the end of the Linked List

public ListNode getNthNodeFromEnd(int n) {
		if(head == null) {
			return null;
		}
		
		if(n <= 0) {
			throw new IllegalArgumentException("Invalid value: n = " + n);
		}
		
		ListNode mainPtr = head;
		ListNode refPtr = head;
		
		int count = 0;
		
		while(count < n) {
			if(refPtr == null) {
				throw new IllegalArgumentException(n + " is greater than the number of nodes in list");
			}
			refPtr = refPtr.next;
			count++;
		}
		
		while(refPtr != null) {
			refPtr = refPtr.next;
			mainPtr = mainPtr.next;
		}
		return mainPtr;
	}

Insert a node in a sorted Linked List

public ListNode insertInSortedList(int value) {
		ListNode newNode = new ListNode(value);
		
		if(head == null) {
			return newNode;
		}
		
		ListNode current = head;
		ListNode temp = null;
		
		while (current != null && current.data < newNode.data) {
			temp = current;
			current = current.next;
		}
		
		newNode.next = current;
		temp.next = newNode;
		return head;
	}

Remove a given key element from a Linked List

public void deleteNode(int key) {
      ListNode current = head;
      ListNode temp = null;
 
      if(current != null && current.data == key){
         head = current.next;
         return;
      }
 
      while(current != null && current.data != key){
         temp = current;
         current = current.next;
      }
 
      if(current == null){
         return;
      }
 
      temp.next = current.next;
   }

Delete a Loop in a Linked List

public class Solution {
            public ListNode detectCycle(ListNode head) {
                ListNode slow = head;
                ListNode fast = head;
        
                while (fast!=null && fast.next!=null){
                    fast = fast.next.next;
                    slow = slow.next;
                    
                    if (fast == slow){
		        // At this point Loop is detected, Now we can introducr slow2 to delete the same.
                        ListNode slow2 = head;
                        // ListNode previous1=null;
                        // ListNode previous2=null;
                        while (slow2 != slow){
                            // previous1=slow;
                            // previous2=slow2;
                            slow = slow.next;
                            slow2 = slow2.next;
                        }
                        return slow;
                    }
                }
                return null;
            }
        }

Reverse a Linked List in group of given size

class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode pointer = dummy;
        while (pointer != null) {
            ListNode node = pointer;
            // first check whether there are k nodes to reverse
            for (int i = 0; i < k && node != null; i++) {
                node = node.next;
            }
            if (node == null) break;
            
            // now we know that we have k nodes, we will start from the first node
            ListNode prev = null, curr = pointer.next, next = null;
            for (int i = 0; i < k; i++) {
                next = curr.next;
                curr.next = prev;
                prev = curr;
                curr = next;
            }
            ListNode tail = pointer.next;
            tail.next = curr;
            pointer.next = prev;
            pointer = tail;
        }
        return dummy.next;
    }
}

Find the Starting point of a Loop in a Linked List

public class Solution {
            public ListNode detectCycle(ListNode head) {
                ListNode slow = head;
                ListNode fast = head;
        
                while (fast!=null && fast.next!=null){
                    fast = fast.next.next;
                    slow = slow.next;
                    
                    if (fast == slow){
                        ListNode slow2 = head;
                        // ListNode previous1=null;
                        // ListNode previous2=null;
                        while (slow2 != slow){
                            // previous1=slow;
                            // previous2=slow2;
                            slow = slow.next;
                            slow2 = slow2.next;
                        }
                        return slow;
                    }
                }
                return null;
            }
        }

About

This repository includes all you need to know about a Linked List

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages