LinkedList

Remove Nth Node From End of List

Given the head of a linked list, remove the nth node from the end of the list and return its head.

Input: head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]
Example 2:

Input: head = [1], n = 1
Output: []
Example 3:

Input: head = [1,2], n = 1
Output: [1]
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution 
{
    public ListNode removeNthFromEnd(ListNode head, int n) 
    {
        if(head.next==null)
            return null;
        
        ListNode slow=head;
        ListNode fast=head;
        
        for(int i=1;i<=n;i++)
            fast=fast.next;
        
        // edge case handeled when we have to delete the 1st node 
        // i.e n=size of linked list
        
        if(fast==null)
            return head.next;
        
        while(fast!=null && fast.next!=null)
        {
            fast=fast.next;
            slow=slow.next;
        }
        
        slow.next=slow.next.next;
        return head;
    }
}

LinkedList Cycle

Delete the Middle Node of a Linked List

Merge two sorted List

Remove Linked List Elements

Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and return the new head.

Middle of the Linked List

Given the head of a singly linked list, return the middle node of the linked list.

If there are two middle nodes, return the second middle node.

Remove Nth Node From End of List

Given the head of a linked list, remove the nth node from the end of the list and return its head.

Singly LinkedList Implementation

Circular LinkedList Implementation

Doubly LinkedList Implementation

Insert into sorted Linkedlist

Middle of Linked List

Reverse a Linked-list Iteratively

Reverse a linked-list recursively

Delete duplicate Item

Floyd Detection Algorithm

Detect and remove the loop in Linked List

Delete node with only pointer given to it

Segregate even & odd nodes in Linked List

Intersection of two linked-list

Pairwise swap in linked-list

Clone a linked list using a random pointer

Clone linked-list using random pointer without hashing

LRU ( least recently used ) Cache Design

Merge two sorted linked lists

Palindrome linked-list

Last updated