Skip to content

Commit 6887bda

Browse files
committed
Remove List Node (#171)
1 parent cd0058f commit 6887bda

38 files changed

Lines changed: 232 additions & 246 deletions

‎circularly_linked_list/insert_cyclic_list.py‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
from typing import Optional
2-
from data_structures.list_node import ListNode
2+
from data_structures.singly_linked_list_node import SinglyLinkedListNode
33

44

5-
def insert(head: Optional[ListNode], value: int) -> Optional[ListNode]:
5+
def insert(head: Optional[SinglyLinkedListNode], value: int) -> Optional[SinglyLinkedListNode]:
66
if not head:
7-
cyclic_list = ListNode(value)
7+
cyclic_list = SinglyLinkedListNode(value)
88
cyclic_list.next = cyclic_list
99
return cyclic_list
1010

1111
previous = head
1212
node = head.next
1313

1414
while node:
15-
if previous.val <= value <= node.val:
16-
new_node = ListNode(value)
15+
if previous.data <= value <= node.data:
16+
new_node = SinglyLinkedListNode(value)
1717
previous.next = new_node
1818
new_node.next = node
1919
return head
20-
if previous.val > node.val:
21-
if value >= previous.val or value <= node.val:
22-
new_node = ListNode(value)
20+
if previous.data > node.data:
21+
if value >= previous.data or value <= node.data:
22+
new_node = SinglyLinkedListNode(value)
2323
previous.next = new_node
2424
new_node.next = node
2525
return head
@@ -30,7 +30,7 @@ def insert(head: Optional[ListNode], value: int) -> Optional[ListNode]:
3030
if previous == head:
3131
break
3232

33-
new_node = ListNode(value)
33+
new_node = SinglyLinkedListNode(value)
3434
previous.next = new_node
3535
new_node.next = node
3636

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
from circularly_linked_list.insert_cyclic_list import insert
2-
from data_structures.list_node import ListNode
2+
from data_structures.singly_linked_list_node import SinglyLinkedListNode
33
from test_helpers.test_helpers import get_cyclic_list_values
44

55

66
def test_insert():
77
head = None
88
assert get_cyclic_list_values(insert(head, 1)) == [1]
99

10-
head = ListNode(1)
10+
head = SinglyLinkedListNode(1)
1111
head.next = head
1212
assert get_cyclic_list_values(insert(head, 2)) == [1, 2]
1313

14-
head = ListNode(3)
15-
head.next = ListNode(4)
16-
head.next.next = ListNode(1)
14+
head = SinglyLinkedListNode(3)
15+
head.next = SinglyLinkedListNode(4)
16+
head.next.next = SinglyLinkedListNode(1)
1717
head.next.next.next = head
1818
assert get_cyclic_list_values(insert(head, 2)) == [3, 4, 1, 2]

‎data_structures/list_node.py‎

Lines changed: 0 additions & 9 deletions
This file was deleted.

‎doubly_linked_list/flatten_doubly_linked_lists.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def __init__(self,
77
previous_node: Optional['Node'],
88
next_node: Optional['Node'],
99
child_node: Optional['Node']):
10-
self.val: int = val
10+
self.data: int = val
1111
self.previous: Optional['Node'] = previous_node
1212
self.next: Optional['Node'] = next_node
1313
self.child: Optional['Node'] = child_node

‎doubly_linked_list/list_node.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55

66
class ListNode(Generic[T]):
77
def __init__(self, val: T, previous_node: Optional['ListNode'] = None, next_node: Optional['ListNode'] = None):
8-
self.value: T = val
8+
self.data: T = val
99
self.previous: Optional[ListNode] = previous_node
1010
self.next: Optional[ListNode] = next_node
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
from typing import Optional
2-
from data_structures.list_node import ListNode
2+
from data_structures.singly_linked_list_node import SinglyLinkedListNode
33

44

5-
def add_two_numbers(head1: Optional[ListNode], head2: Optional[ListNode]) -> Optional[ListNode]:
5+
def add_two_numbers(head1: Optional[SinglyLinkedListNode],
6+
head2: Optional[SinglyLinkedListNode]) -> Optional[SinglyLinkedListNode]:
67
node1 = head1
78
node2 = head2
8-
dummy_node = ListNode(0)
9+
dummy_node = SinglyLinkedListNode(0)
910
result = dummy_node
1011
carry = 0
1112
while node1 or node2:
1213
val1 = 0
1314
val2 = 0
1415
if node1:
15-
val1 = node1.val
16+
val1 = node1.data
1617
node1 = node1.next
1718
if node2:
18-
val2 = node2.val
19+
val2 = node2.data
1920
node2 = node2.next
2021

2122
total = val1 + val2 + carry
2223
carry = int(total / 10)
23-
result.next = ListNode(total % 10)
24+
result.next = SinglyLinkedListNode(total % 10)
2425
result = result.next
2526

2627
if carry > 0:
27-
result.next = ListNode(carry)
28+
result.next = SinglyLinkedListNode(carry)
2829

2930
return dummy_node.next

‎singly_linked_lists/delete_duplicates.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from typing import Optional
2-
from data_structures.list_node import ListNode
2+
from data_structures.singly_linked_list_node import SinglyLinkedListNode
33

44

5-
def delete_duplicates(head: Optional[ListNode]) -> Optional[ListNode]:
5+
def delete_duplicates(head: Optional[SinglyLinkedListNode]) -> Optional[SinglyLinkedListNode]:
66
node = head
77

88
while node:
9-
if node.next and node.val == node.next.val:
9+
if node.next and node.data == node.next.data:
1010
node.next = node.next.next
1111
else:
1212
node = node.next

‎singly_linked_lists/detect_cycle.py‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
from typing import Optional, Set
2-
from data_structures.list_node import ListNode
2+
from data_structures.singly_linked_list_node import SinglyLinkedListNode
33

44

5-
def detect_cycle(head: ListNode) -> Optional[ListNode]:
5+
def detect_cycle(head: SinglyLinkedListNode) -> Optional[SinglyLinkedListNode]:
66
# detect cycle
77
if not head or not head.next:
88
return None
9-
slow: ListNode = head
10-
fast: ListNode = head
11-
intersection: Optional[ListNode] = None
9+
slow: SinglyLinkedListNode = head
10+
fast: SinglyLinkedListNode = head
11+
intersection: Optional[SinglyLinkedListNode] = None
1212

1313
while fast and fast.next:
1414
slow = slow.next
@@ -29,8 +29,8 @@ def detect_cycle(head: ListNode) -> Optional[ListNode]:
2929
return intersection
3030

3131

32-
def detect_cycle_with_set(head: ListNode) -> Optional[ListNode]:
33-
visited_nodes: Set[ListNode] = set()
32+
def detect_cycle_with_set(head: SinglyLinkedListNode) -> Optional[SinglyLinkedListNode]:
33+
visited_nodes: Set[SinglyLinkedListNode] = set()
3434

3535
node = head
3636

‎singly_linked_lists/find_middle_node.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from typing import Optional
2-
from data_structures.list_node import ListNode
2+
from data_structures.singly_linked_list_node import SinglyLinkedListNode
33

44

5-
def find_middle_node(head: Optional[ListNode]) -> Optional[ListNode]:
5+
def find_middle_node(head: Optional[SinglyLinkedListNode]) -> Optional[SinglyLinkedListNode]:
66
if not head:
77
return None
88
slow = head

‎singly_linked_lists/intersection_two_linked_lists.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from typing import Optional
2-
from data_structures.list_node import ListNode
2+
from data_structures.singly_linked_list_node import SinglyLinkedListNode
33

44

5-
def get_intersection_node(head_a: ListNode, head_b: ListNode) -> Optional[ListNode]:
5+
def get_intersection_node(head_a: SinglyLinkedListNode, head_b: SinglyLinkedListNode) -> Optional[SinglyLinkedListNode]:
66
if not head_a or not head_b:
77
return None
88

9-
node_a: ListNode = head_a
10-
node_b: ListNode = head_b
9+
node_a: SinglyLinkedListNode = head_a
10+
node_b: SinglyLinkedListNode = head_b
1111

1212
while node_a != node_b:
1313
node_a = node_a.next

0 commit comments

Comments
 (0)