Contents

Remove Nth Node From End of List | One Pass Time O(n) Space O(1) Solution Illustration

Intuition

featured-image.jpg
Remove Nth Node From End of List

Complexity

  • Time complexity: O(n)
  • Space complexity: O(1)

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next

# one pass
class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
        mv_tail = mv_head = head
        
        while n > 0:
            n -= 1
            mv_head = mv_head.next

        # when n == len(LL)
        # handle head = [1], n = 1, output = []
        # handle head = [1, 2], n = 2, output = [2]
        if mv_head is None:
            return head.next

        while mv_head.next is not None:
            mv_tail = mv_tail.next
            mv_head = mv_head.next
            
        mv_tail.next = mv_tail.next.next

        return head