-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path174.py
37 lines (32 loc) · 845 Bytes
/
174.py
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
"""
Definition of ListNode
class ListNode(object):
def __init__(self, val, next=None):
self.val = val
self.next = next
"""
class Solution:
"""
@param head: The first node of linked list.
@param n: An integer
@return: The head of linked list.
"""
def removeNthFromEnd(self, head, n):
# write your code here
if head == None or n < 0:
return head
lf, rg = head, head
for i in range(n):
rg = rg.next
if rg == None:
delNode = head
head = head.next
del delNode
return head
while rg.next != None:
rg = rg.next
lf = lf.next
delNode = lf.next
lf.next = delNode.next
del delNode
return head