-
Notifications
You must be signed in to change notification settings - Fork 622
/
Copy pathremove_nth_ll.py
69 lines (50 loc) · 1.54 KB
/
remove_nth_ll.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
'''
Remove Nth Node From End of List
Given a linked list, remove the n-th node from the end of list and return its head.
Input: 1 -> 2 -> 3 -> 4 -> 5, 2.
Output: 1 -> 2 -> 3 -> 5
=========================================
Playing with the pointers.
Time Complexity: O(N)
Space Complexity: O(1)
Recursive solution.
Time Complexity: O(N)
Space Complexity: O(N) , because of the recursive calls stack
'''
##############
# Solution 1 #
##############
# import ListNode class from ll_helpers.py
from ll_helpers import ListNode
def remove_nth_from_end_1(head, n):
helper = ListNode(0)
helper.next = head
first = helper
second = helper
# count to N with the first pointer
for i in range(n + 1):
first = first.next
# go (Length - N) elements with first pointer
# and in that way the second pointer will be Nth from the end
while first != None:
first = first.next
second = second.next
# remove the element (change the next pointer from the previous element)
second.next = second.next.next
return helper.next
##############
# Solution 2 #
##############
def remove_nth_from_end_2(head, n):
result = remove_recursively(head, n)
if result[0] == n:
return head.next
return head
def remove_recursively(pointer, n):
if pointer is None:
return (0, None)
# go to the end and count how many are there
result = remove_recursively(pointer.next, n)
if result[0] == n:
pointer.next = result[1]
return (result[0] + 1, pointer.next)