-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path19. Remove Nth Node From End of List.py
More file actions
62 lines (60 loc) · 1.39 KB
/
19. Remove Nth Node From End of List.py
File metadata and controls
62 lines (60 loc) · 1.39 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
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
'''
class Solution(object):
def removeNthFromEnd(self, head, n):
"""
:type head: ListNode
:type n: int
:rtype: ListNode
"""
num = 0
tmp = head
while tmp != None:
num = num + 1
tmp = tmp.next
n = num - n
present = head
if n == 0:
if num == 1:
return []
return head.next
for i in range(0,n):
last = present
present = present.next
fol = present.next
last.next = fol
del present
return head
'''
class Solution(object):
def removeNthFromEnd(self, head, n):
"""
:type head: ListNode
:type n: int
:rtype: ListNode
"""
if n == 0:
return []
p1 = head
p2 = head
for i in range(0,n - 1):
p1 = p1.next
if p1.next == None:
return head.next
while p1.next != None:
last = p2
p2 = p2.next
p1 = p1.next
last.next = p2.next
return head
if __name__ == '__main__':
solution = Solution()
t = ListNode(1)
t2 = ListNode(2)
t.next = t2
re = solution.removeNthFromEnd(t,1)
print re