Skip to content

Commit c3af296

Browse files
committed
constant time tail insertion
1 parent cb4a265 commit c3af296

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

Python/chapter02/2.1 - Remove Dups/miguel_2.1_sol.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,19 @@ def __init__(self, d: int):
1919
class LinkedList:
2020
def __init__(self, initial_value: int = None):
2121
self.head = None
22+
self.tail = None
2223
if initial_value is not None:
2324
self.head = Node(initial_value)
25+
self.tail = self.head
2426

2527
def append_to_tail(self, d: int) -> None:
2628
if self.head is None:
2729
self.head = Node(d)
30+
self.tail = self.head
2831
return
2932
end = Node(d)
30-
n = self.head
31-
while n.next is not None:
32-
n = n.next
33-
n.next = end
33+
self.tail.next = end
34+
self.tail = end
3435

3536
def append_to_head(self, d: int) -> None:
3637
new_head = Node(d)

0 commit comments

Comments
 (0)