Skip to content

Commit 5c91b7f

Browse files
committed
committed from zkp
1 parent d3ee417 commit 5c91b7f

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

LeetCode/removeNthFromEnd.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Definition for singly-linked list.
2+
# class ListNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution(object):
8+
def removeNthFromEnd(self, head, n):
9+
"""
10+
:type head: ListNode
11+
:type n: int
12+
:rtype: ListNode
13+
"""
14+
curnode = head
15+
s = []
16+
while curnode:
17+
s.append(id(curnode))
18+
curnode = curnode.next
19+
t = s[-n]
20+
if id(head) == t:
21+
head = head.next
22+
else:
23+
d_curnode = head
24+
while d_curnode:
25+
if id(d_curnode.next) == t:
26+
d = d_curnode.next
27+
d_curnode.next = d.next
28+
del d
29+
break
30+
d_curnode = d_curnode.next
31+
return head

0 commit comments

Comments
 (0)