-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathleetcodeQuestion19.py
More file actions
35 lines (33 loc) · 835 Bytes
/
leetcodeQuestion19.py
File metadata and controls
35 lines (33 loc) · 835 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
32
33
34
35
class ListNode:
def __init__(self, x, next=None):
self.val = x
self.next = next
class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
if head is None:
return None
if head.next is None:
return None
if n == 0:
return head
p = head
q = head
for i in range(n):
if q is None:
return head
q = q.next
if q is None:
return head.next
while q.next is not None:
p = p.next
q = q.next
p.next = p.next.next
return head
if __name__ == "__main__":
head = ListNode(1)
s = Solution()
head = s.removeNthFromEnd(head, 1)
p = head
while p is not None:
print(p.val)
p = p.next