Given the head
of a linked list, remove the nth
node from the end of the list and return its head.
Example 1:
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]
Constraints:
sz
.1 <= sz <= 30
0 <= Node.val <= 100
1 <= n <= sz
Follow up: Could you do this in one pass?
Given the head
of a linked list, remove the n<sup>th</sup>
node from the end of the list and return its head.
This problem can be efficiently solved using the two-pointer technique. We'll use two pointers, fast
and slow
, both initially pointing to a dummy node prepended to the head of the list. This dummy node simplifies handling the case where the first node needs to be removed.
fast
and slow
to point to it.fast
: Move the fast
pointer n
steps forward. This creates a gap of n
nodes between fast
and slow
.fast
and slow
pointers one step at a time until fast
reaches the end of the list. At this point, slow
will be pointing to the node before the n
th node from the end.n
th node from the end by setting slow.next
to slow.next.next
.next
pointer of the dummy node (the head of the modified list).# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
dummy = ListNode(next=head) # Dummy node simplifies edge cases
fast = slow = dummy
# Move fast pointer n steps ahead
for _ in range(n):
fast = fast.next
if fast is None: #Handle edge case where n is larger than list length
return head
# Move both pointers until fast reaches the end
while fast.next:
fast = fast.next
slow = slow.next
# Remove the nth node from the end
slow.next = slow.next.next
return dummy.next # Return the head of the modified list
The implementations in other languages (Java, C++, Go, TypeScript, Rust, Javascript, Swift, Ruby, C#, PHP) follow the same logic, differing only in syntax and data structures. The core algorithm remains consistent. Refer to the original response for the complete code examples in these languages.