|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from dataclasses import dataclass |
| 4 | +from typing import Any |
| 5 | + |
| 6 | + |
| 7 | +@dataclass |
| 8 | +class Node: |
| 9 | + item: Any |
| 10 | + next: Node | Any = None |
| 11 | + |
| 12 | + |
| 13 | +class LinkedList: |
| 14 | + def __init__(self) -> None: |
| 15 | + self.head: Node | None = None |
| 16 | + self.size = 0 |
| 17 | + |
| 18 | + def __str__(self) -> str: |
| 19 | + """ |
| 20 | + >>> linked_list = LinkedList() |
| 21 | + >>> linked_list.add(23) |
| 22 | + >>> linked_list.add(14) |
| 23 | + >>> linked_list.add(9) |
| 24 | + >>> print(linked_list) |
| 25 | + 9 --> 14 --> 23 |
| 26 | + """ |
| 27 | + iterate = self.head |
| 28 | + item_str = "" |
| 29 | + item_list: list[str] = [] |
| 30 | + while iterate: |
| 31 | + item_list.append(str(iterate.item)) |
| 32 | + iterate = iterate.next |
| 33 | + |
| 34 | + item_str = " --> ".join(item_list) |
| 35 | + |
| 36 | + return item_str |
| 37 | + |
| 38 | + def __len__(self) -> int: |
| 39 | + """ |
| 40 | + >>> linked_list = LinkedList() |
| 41 | + >>> len(linked_list) |
| 42 | + 0 |
| 43 | + >>> linked_list.add("a") |
| 44 | + >>> len(linked_list) |
| 45 | + 1 |
| 46 | + >>> linked_list.add("b") |
| 47 | + >>> len(linked_list) |
| 48 | + 2 |
| 49 | + """ |
| 50 | + return self.size |
| 51 | + |
| 52 | + def add(self, item: Any, position: int = 0) -> None: |
| 53 | + """ |
| 54 | + Add an item to the LinkedList at the specified position. |
| 55 | + Default position is 0 (the head). |
| 56 | +
|
| 57 | + Args: |
| 58 | + item (Any): The item to add to the LinkedList. |
| 59 | + position (int, optional): The position at which to add the item. |
| 60 | + Defaults to 0. |
| 61 | +
|
| 62 | + Raises: |
| 63 | + ValueError: If the position is negative or out of bounds. |
| 64 | +
|
| 65 | + >>> linked_list = LinkedList() |
| 66 | + >>> linked_list.add(1) |
| 67 | + >>> linked_list.add(2) |
| 68 | + >>> linked_list.add(3) |
| 69 | + >>> linked_list.add(4, 2) |
| 70 | + >>> print(linked_list) |
| 71 | + 3 --> 2 --> 4 --> 1 |
| 72 | +
|
| 73 | + # Test adding to a negative position |
| 74 | + >>> linked_list.add(5, -3) |
| 75 | + Traceback (most recent call last): |
| 76 | + ... |
| 77 | + ValueError: Position must be non-negative |
| 78 | +
|
| 79 | + # Test adding to an out-of-bounds position |
| 80 | + >>> linked_list.add(5,7) |
| 81 | + Traceback (most recent call last): |
| 82 | + ... |
| 83 | + ValueError: Out of bounds |
| 84 | + >>> linked_list.add(5, 4) |
| 85 | + >>> print(linked_list) |
| 86 | + 3 --> 2 --> 4 --> 1 --> 5 |
| 87 | + """ |
| 88 | + if position < 0: |
| 89 | + raise ValueError("Position must be non-negative") |
| 90 | + |
| 91 | + if position == 0 or self.head is None: |
| 92 | + new_node = Node(item, self.head) |
| 93 | + self.head = new_node |
| 94 | + else: |
| 95 | + current = self.head |
| 96 | + for _ in range(position - 1): |
| 97 | + current = current.next |
| 98 | + if current is None: |
| 99 | + raise ValueError("Out of bounds") |
| 100 | + new_node = Node(item, current.next) |
| 101 | + current.next = new_node |
| 102 | + self.size += 1 |
| 103 | + |
| 104 | + def partition_liked_list(self, value: int) -> None: |
| 105 | + """ |
| 106 | + Partition Linked List based on node elements in-order. |
| 107 | + All nodes with elements less than value should occur in the left, |
| 108 | + while those greater than to value, in the right. |
| 109 | +
|
| 110 | + >>> linked_list = LinkedList() |
| 111 | + >>> linked_list.add(1) |
| 112 | + >>> linked_list.add(2) |
| 113 | + >>> linked_list.add(3) |
| 114 | + >>> linked_list.add(4, 2) |
| 115 | + >>> linked_list.add(5, 4) |
| 116 | + >>> print(linked_list) |
| 117 | + 3 --> 2 --> 4 --> 1 --> 5 |
| 118 | + >>> linked_list.partition_liked_list(3) |
| 119 | + >>> print(linked_list) |
| 120 | + 2 --> 1 --> 3 --> 4 --> 5 |
| 121 | + >>> linked_list = LinkedList() |
| 122 | + >>> linked_list.add(1) |
| 123 | + >>> linked_list.add(2) |
| 124 | + >>> linked_list.add(3) |
| 125 | + >>> print(linked_list) |
| 126 | + 3 --> 2 --> 1 |
| 127 | + >>> linked_list.partition_liked_list(3) |
| 128 | + >>> print(linked_list) |
| 129 | + 2 --> 1 --> 3 |
| 130 | + >>> linked_list = LinkedList() |
| 131 | + >>> linked_list.add(5) |
| 132 | + >>> linked_list.add(5) |
| 133 | + >>> linked_list.add(5) |
| 134 | + >>> print(linked_list) |
| 135 | + 5 --> 5 --> 5 |
| 136 | + >>> linked_list.partition_liked_list(5) |
| 137 | + >>> print(linked_list) |
| 138 | + 5 --> 5 --> 5 |
| 139 | + >>> linked_list = LinkedList() |
| 140 | + >>> linked_list.add(1, 0) |
| 141 | + >>> linked_list.add(2, 1) |
| 142 | + >>> linked_list.add(3, 2) |
| 143 | + >>> linked_list.add(4, 3) |
| 144 | + >>> linked_list.add(5, 4) |
| 145 | + >>> print(linked_list) |
| 146 | + 1 --> 2 --> 3 --> 4 --> 5 |
| 147 | + >>> linked_list.partition_liked_list(6) |
| 148 | + >>> print(linked_list) |
| 149 | + 1 --> 2 --> 3 --> 4 --> 5 |
| 150 | + >>> linked_list = LinkedList() |
| 151 | + >>> linked_list.add(1) |
| 152 | + >>> print(linked_list) |
| 153 | + 1 |
| 154 | + >>> linked_list.partition_liked_list(1) |
| 155 | + >>> print(linked_list) |
| 156 | + 1 |
| 157 | + """ |
| 158 | + if self.head is None: |
| 159 | + return None |
| 160 | + |
| 161 | + less_nodes, greater_nodes = Node(0), Node(0) |
| 162 | + current, current_less, current_greater = self.head, less_nodes, greater_nodes |
| 163 | + |
| 164 | + while current is not None: |
| 165 | + if current.item < value: |
| 166 | + current_less.next = current |
| 167 | + current_less = current_less.next |
| 168 | + else: |
| 169 | + current_greater.next = current |
| 170 | + current_greater = current_greater.next |
| 171 | + current = current.next |
| 172 | + |
| 173 | + current_less.next = greater_nodes.next |
| 174 | + current_greater.next = None |
| 175 | + self.head = less_nodes.next |
0 commit comments