-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path019_RemoveNthFromEnd.py
76 lines (66 loc) · 2 KB
/
019_RemoveNthFromEnd.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
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
68
69
70
71
72
73
74
75
76
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
#Other solutions provided by LeetCode
class Solution:
def removeNthFromEnd(self, head, n):
def index(node):
if not node:
return 0
i = index(node.next) + 1
if i > n:
node.next.val = node.val
return i
index(head)
return head.next
class Solution:
def removeNthFromEnd(self, head, n):
def remove(head):
if not head:
return 0, head
i, head.next = remove(head.next)
return i+1, (head, head.next)[i+1 == n]
return remove(head)[1]
class Solution:
def removeNthFromEnd(self, head, n):
fast = slow = head
for _ in range(n):
fast = fast.next
if not fast:
return head.next
while fast.next:
fast = fast.next
slow = slow.next
slow.next = slow.next.next
return head
#54.06%
class Solution(object):
def removeNthFromEnd(self, head, n):
"""
:type head: ListNode
:type n: int
:rtype: ListNode
"""
def nextStep(node, n):
'''
Move the pointer to next node and n--.
'''
if node is None or n == 0:
return node, n
else:
return nextStep(node.next, n - 1)
if head is None or n <= 0:
return head
#Two nodes track the node list
nodeRet, nRet = nextStep(head, n)
if nodeRet is None:
if nRet == 0:
head = head.next
return head
node1 = head
while nodeRet.next is not None:
nodeRet, node1 = nodeRet.next, node1.next
node1.next = node1.next.next
return head