-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ19.py
More file actions
28 lines (25 loc) · 701 Bytes
/
Q19.py
File metadata and controls
28 lines (25 loc) · 701 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
from ListNode import ListNode
class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
if head is None:
return head
if head.next is None:
return None
p = head
q = None
count = 1
while p.next!=None:
count+=1
p = p.next
p = head
if count is n:
return p.next
for i in range(count-n):
q = p
p = p.next
q.next = p.next
return head
if __name__ == '__main__':
arr = [1,2]
head = ListNode.initNodeList(arr)
ListNode.linkListPrint(Solution().removeNthFromEnd(head,2))