-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path19.py
More file actions
31 lines (24 loc) · 746 Bytes
/
19.py
File metadata and controls
31 lines (24 loc) · 746 Bytes
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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
window = []
temp = head
# Preload window
for i in range(n+1):
if temp is None:
return head.next
window.append(temp)
temp = temp.next
while temp is not None:
window.pop(0)
window.append(temp)
temp = temp.next
node = window[0]
if node.next is None:
return None
node.next = node.next.next
return head