-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoveNthFromEnd.py
More file actions
31 lines (30 loc) · 824 Bytes
/
removeNthFromEnd.py
File metadata and controls
31 lines (30 loc) · 824 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
# 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
"""
curnode = head
s = []
while curnode:
s.append(id(curnode))
curnode = curnode.next
t = s[-n]
if id(head) == t:
head = head.next
else:
d_curnode = head
while d_curnode:
if id(d_curnode.next) == t:
d = d_curnode.next
d_curnode.next = d.next
del d
break
d_curnode = d_curnode.next
return head