↓Skip to main content
  1. LeetCode/

LeetCode 19: Remove Nth Node From End of List

·2 mins· ·
LeetCode Medium Linked-List Two-Pointers
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2022-10-20

  • Submissions: Accepted → Runtime Error → Accepted ×2 → Runtime Error ×2 → Wrong Answer ×4 → Accepted → Runtime Error

Approach
#

Fast goes first so the two pointers keep a fixed gap; when fast reaches the end, slow is just before the n-th node from the end (Linked List Template 4). Starting from a dummy means deleting the head (n equal to the length) needs no special case.

My Attempts
#

The first version passed, but later rewrites kept failing when deleting the head: without a dummy I had to special-case “fast reaches the end after n steps”, and I tried return [], ListNode([]), ListNode(None) and ListNode(0), none of which is an empty list. For [3], n = 1 the answer is an empty list. A dummy removes the special case.

Solution
#

class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
        dummy = ListNode(0, head)
        fast = slow = dummy
        for _ in range(n + 1):
            fast = fast.next
        while fast:
            fast, slow = fast.next, slow.next
        slow.next = slow.next.next
        return dummy.next

Complexity
#

Time O(n), space O(1), in one pass.

Mistakes to Avoid
#

  • When the head is deleted, return None (an empty list), not [], ListNode([]) or ListNode(0).
  • Starting from a dummy removes the special case for deleting the head.

Interview Explanation
#

From a dummy, fast takes n + 1 steps first, then both move until fast is None; slow is then before the target, which I skip. One pass, time O(n).

Related

LeetCode 11: Container With Most Water
·2 mins
LeetCode Medium Array Two-Pointers Greedy
LeetCode 11 note: start pointers at both ends and always move the shorter side; moving the taller side only narrows the width without raising the height
LeetCode 15: 3Sum
·2 mins
LeetCode Medium Array Two-Pointers Sorting
LeetCode 15 note: sort, fix the first number, and find the other two with converging pointers, skipping equal values at all three positions to avoid duplicate triplets
LeetCode 16: 3Sum Closest
·2 mins
LeetCode Medium Array Two-Pointers Sorting
LeetCode 16 note: sort, fix one number, and find the other two with converging pointers, comparing the distance abs(sum - target)
LeetCode 18: 4Sum
·2 mins
LeetCode Medium Array Two-Pointers Sorting
LeetCode 18 note: sort, fix the first two numbers with two loops, and converge pointers for the rest, skipping equal values at all four positions
LeetCode 5: Longest Palindromic Substring
·2 mins
LeetCode Medium Two-Pointers String Dynamic-Programming Manacher
LeetCode 5 note: expand around centers, using each position as an odd and an even center; with interval DP, iterate i from large to small
LeetCode 876: Middle of the Linked List
·2 mins
LeetCode Easy Linked-List Two-Pointers
LeetCode 876 note: fast moves two steps and slow one; when fast reaches the end slow is in the middle, the right-middle for even lengths