|
| 1 | +class Node: |
| 2 | + """ |
| 3 | + A node in a double linked list. |
| 4 | +
|
| 5 | + Each node supports a data, previous, and next |
| 6 | + - data: the value |
| 7 | + - previous: pointer to the previous node |
| 8 | + - next: pointer to the next node |
| 9 | + """ |
| 10 | + |
| 11 | + def __init__(self, data): |
| 12 | + self.data = data |
| 13 | + self.previous = None |
| 14 | + self.next = None |
| 15 | + |
| 16 | +class LinkedList: |
| 17 | + """ |
| 18 | + Double Linked List |
| 19 | +
|
| 20 | + Supports O(1) operations on insertion at head, removal from tail, and removal from arbitrary node |
| 21 | + """ |
| 22 | + def __init__(self): |
| 23 | + # Head points to the first node in the list |
| 24 | + self.head = None |
| 25 | + |
| 26 | + # Tail points to the last node in the list |
| 27 | + self.tail = None |
| 28 | + |
| 29 | + def is_empty(self): |
| 30 | + """Returns True if the list contains no nodes.""" |
| 31 | + return self.head is None |
| 32 | + |
| 33 | + def push_head(self, data): |
| 34 | + """ |
| 35 | + Insert a new node at the head of the list. |
| 36 | +
|
| 37 | + Time Complexity: O(1) |
| 38 | +
|
| 39 | + Returns: |
| 40 | + Node: reference to the inserted node (handle for later removal) |
| 41 | + """ |
| 42 | + node = Node(data) |
| 43 | + |
| 44 | + if self.is_empty(): |
| 45 | + # If list is empty, head and tail both point to new node |
| 46 | + self.head = node |
| 47 | + self.tail = node |
| 48 | + else: |
| 49 | + # Link new node before current head |
| 50 | + node.next = self.head |
| 51 | + self.head.previous = node |
| 52 | + |
| 53 | + # Update head pointer |
| 54 | + self.head = node |
| 55 | + |
| 56 | + return node |
| 57 | + |
| 58 | + def pop_tail(self): |
| 59 | + """ |
| 60 | + Remove and return the value at the tail of the list. |
| 61 | +
|
| 62 | + Time Complexity: O(1) |
| 63 | +
|
| 64 | + Raises: |
| 65 | + ValueError: if the list is empty |
| 66 | + """ |
| 67 | + |
| 68 | + if self.is_empty(): |
| 69 | + raise ValueError("List is empty.") |
| 70 | + |
| 71 | + # store value before removing node |
| 72 | + value = self.tail.data |
| 73 | + |
| 74 | + if self.head == self.tail: |
| 75 | + # Only one element in the list |
| 76 | + self.head = None |
| 77 | + self.tail = None |
| 78 | + |
| 79 | + else: |
| 80 | + # Move tail pointer backwards |
| 81 | + self.tail = self.tail.previous |
| 82 | + |
| 83 | + # Disconnect old tail node |
| 84 | + self.tail.next = None |
| 85 | + |
| 86 | + return value |
| 87 | + |
| 88 | + def remove(self, node): |
| 89 | + """ |
| 90 | + Remove a node from the list using its reference. |
| 91 | +
|
| 92 | + Time Complexity: O(1) |
| 93 | +
|
| 94 | + Args: |
| 95 | + node (Node): the node to remove |
| 96 | + """ |
| 97 | + if node.previous: |
| 98 | + node.previous.next = node.next |
| 99 | + else: |
| 100 | + self.head = node.next |
| 101 | + |
| 102 | + if node.next: |
| 103 | + node.next.previous = node.previous |
| 104 | + else: |
| 105 | + self.tail = node.previous |
| 106 | + |
| 107 | + node.previous = None |
| 108 | + node.next = None |
0 commit comments