-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0019.py
More file actions
90 lines (59 loc) · 1.95 KB
/
Copy path0019.py
File metadata and controls
90 lines (59 loc) · 1.95 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""
- https://leetcode.com/problems/remove-nth-node-from-end-of-list/
- Classification: Fast & Slow Pointers
## Challenge
To remove the n-th element from the end of the list
## Solution: Using the fast and slow pointer pattern.
- The fast pointer will start with a gap of n away from slow.
- Both pointers will move at the same speed till the fast pointer
reaches the end of the list (fast.next is None)
- Skip the nth element by connecting the slow.next to slow.next.next
## Example
remove n = 3 from a list
n: 6 5 4 3 2 1
l: [1, 2, 3, 4, 5, 6]
Step 1: Start fast and slow at the head of the list:
* (fast)
n: 6 5 4 3 2 1
l: [1, 2, 3, 4, 5, 6]
# (slow)
Step 2: create gap n
*
n: 6 5 4 3 2 1
l: [1, 2, 3, 4, 5, 6]
#
Step 3: move both pointers till fast reaches end
(while fast.next: move pointers)
the slow pointer is now at n-1
*
n: 6 5 4 3 2 1
l: [1, 2, 3, 4, 5, 6]
#
Step 4: skip #.next (node 3 from the end)
return the head of the adjusted list
## Edge Case: n equals length of list,
Solution: skip the first element and return head.next
Example: l: [1], n = 1
After step 1 and 2 the pointers will look like:
# is None: the while loop will throw an exception
[1]
*
"""
#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: ListNode, n: int) -> ListNode:
fast = slow = head
for _ in range(n): # Create the gap
fast = fast.next
try:
while fast.next: # Move till end
slow = slow.next
fast = fast.next
slow.next = slow.next.next # Remove the n-th node
return head
except:
return head.next # n is list length, skip the first element