Skip to content

Latest commit

 

History

History
33 lines (32 loc) · 781 Bytes

DAY3P1.md

File metadata and controls

33 lines (32 loc) · 781 Bytes

Remove Nth Node From End of List


  • Question:

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


  • Example:

alt

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

Output: [1,2,3,5]


  • Solution:

Code :

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode start=new ListNode(0);
        ListNode slow=start,fast=start;
        slow.next=head;
        for(int i=1;i<=n+1;i++)
        {
            fast=fast.next;
        }
        while(fast!=null)
        {
            slow=slow.next;
            fast=fast.next;
        }
        slow.next=slow.next.next;
        return start.next;
        
    }
}