Remove Nth node from the back of the LL

Problem Statement

Given the head of a singly linked list and an integer n. Remove the nth node from the back of the linked List and return the head of the modified list. The value of n will always be less than or equal to the number of nodes in the linked list.

LinkedList Node

class Node: def __init__(self, key): self.val = val self.next = None

Example

1 --> 2 --> 3 --> 4 --> 5 & n = 2 Output: 1 --> 2 --> 3 --> 5 1 --> 2 --> 3 --> 4 --> 5 & n = 4 Output: 1 --> 3 --> 4 --> 5 1 --> 2 --> 3 --> 4 --> 5 & n = 6 Output: 1 --> 2 --> 3 --> 4 --> 5

Approach (Two Pointers)

Code

def deleteNthNodeFromEnd(head, n): dummy = ListNode(0) dummy.next = head first = dummy second = dummy # Move first n+1 steps ahead so second is just before the node to delete for _ in range(n + 1): if not first: return head # n is greater than the length of the list first = first.next # Move both pointers until first reaches the end while first: first = first.next second = second.next # Delete the nth node from the end second.next = second.next.next return dummy.next

Complexity Analysis