-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove_nth_from_end.py
148 lines (115 loc) · 3.04 KB
/
remove_nth_from_end.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
'''
Given the head of a linked list, remove the nth node from the end of the list and return its head.
Example 1:
Input: head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]
Example 2:
Input: head = [1], n = 1
Output: []
Example 3:
Input: head = [1,2], n = 1
Output: [1]
Constraints:
The number of nodes in the list is sz.
1 <= sz <= 30
0 <= Node.val <= 100
1 <= n <= sz
Follow up: Could you do this in one pass?
'''
class ListNode(object):
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def linkedListString(head):
current_node = head
linked_list_str = '['
while current_node:
linked_list_str += str(current_node.val)
if current_node.next:
linked_list_str += ', '
current_node = current_node.next
linked_list_str += ']'
return linked_list_str
# The brute force way
def removeNthFromEndBF(head, n):
"""
:type head: ListNode
:type n: int
:rtype: ListNode
"""
# Get length of linked list
length = 0
curr = head
while curr:
length += 1
curr = curr.next
# Index of the element to remove
ind = length - n
if length != 1:
if ind == 0:
head = head.next
else:
# Traverse the list until element and remove it
curr = head
for i in range(ind):
if i == ind - 1:
curr.next = curr.next.next
curr = curr.next
else:
head = None
return head
def reverseLinkedList(head):
prev = None
current = head
while current is not None:
next = current.next
current.next = prev
prev = current
current = next
head = prev
return head
def removeNthFromEndReverse(head, n):
"""
:type head: ListNode
:type n: int
:rtype: ListNode
"""
# Reverse the linked list
head = reverseLinkedList(head)
# Remove the nth node from the end
prev = None
curr = head
for _ in range(n - 1):
prev = curr
curr = curr.next
if prev:
prev.next = curr.next
else:
head = head.next
# Reverse the linked list again
head = reverseLinkedList(head)
return head
# Some Test cases
if __name__ == "__main__":
node5 = ListNode(5)
node4 = ListNode(4, node5)
node3 = ListNode(3, node4)
node2 = ListNode(2, node3)
head1 = ListNode(1, node2)
head2 = ListNode(1)
node2 = ListNode(2)
head3 = ListNode(1, node2)
node2 = ListNode(2)
head4 = ListNode(1, node2)
# Examples
input = [
(head1, 2, [1,2,3,5]),
(head2, 1, []),
(head3, 1, [1]),
(head4, 2, [2])
]
for idx, (head, n, expected) in enumerate(input):
print("\nExample", idx, ":")
print("Input:", linkedListString(head), n)
print("Output:", linkedListString(removeNthFromEndReverse(head, n)))
print("Expected:", expected)