-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.py
More file actions
67 lines (43 loc) · 1.09 KB
/
Solution.py
File metadata and controls
67 lines (43 loc) · 1.09 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
__author__ = 'cfwloader'
# 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
"""
if head is None:
return head
if n == 1:
node = head.next
if node is None:
return node
preNode = head
while node.next is not None:
preNode = node
node = node.next
preNode.next = None
return head
dict = {}
index = 1
iterator = head
while iterator is not None:
dict[index] = iterator
iterator = iterator.next
index += 1
target = dict[index - n]
if target == head:
return target.next
else:
dict[index - n - 1].next = target.next
return head
if __name__ == '__main__':
'''
[1,2,3]
2
'''