Skip to content

Commit bbf7f99

Browse files
committed
Update Readme
1 parent 2580259 commit bbf7f99

2 files changed

Lines changed: 48 additions & 48 deletions

File tree

‎circularly_linked_list/README.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ If the list is empty (i.e., given node is null), you should create a new single
1515
to that single node. Otherwise, you should return the original given node.
1616

1717
```python
18-
def insert(head: Optional[ListNode], value: int) -> Optional[ListNode]:
18+
def insert(head: Optional[SinglyLinkedListNode], value: int) -> Optional[SinglyLinkedListNode]:
1919
if not head:
20-
cyclic_list = ListNode(value)
20+
cyclic_list = SinglyLinkedListNode(value)
2121
cyclic_list.next = cyclic_list
2222
return cyclic_list
2323

@@ -26,13 +26,13 @@ def insert(head: Optional[ListNode], value: int) -> Optional[ListNode]:
2626

2727
while node:
2828
if previous.val <= value <= node.val:
29-
new_node = ListNode(value)
29+
new_node = SinglyLinkedListNode(value)
3030
previous.next = new_node
3131
new_node.next = node
3232
return head
3333
if previous.val > node.val:
3434
if value >= previous.val or value <= node.val:
35-
new_node = ListNode(value)
35+
new_node = SinglyLinkedListNode(value)
3636
previous.next = new_node
3737
new_node.next = node
3838
return head
@@ -43,7 +43,7 @@ def insert(head: Optional[ListNode], value: int) -> Optional[ListNode]:
4343
if previous == head:
4444
break
4545

46-
new_node = ListNode(value)
46+
new_node = SinglyLinkedListNode(value)
4747
previous.next = new_node
4848
new_node.next = node
4949

‎singly_linked_lists/README.md‎

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Explanation: `342 + 465 = 807`
3232
```python
3333
node1 = head1
3434
node2 = head2
35-
dummy_node = ListNode(0)
35+
dummy_node = SinglyLinkedListNode(0)
3636
result = dummy_node
3737
carry = 0
3838
while node1 or node2:
@@ -47,11 +47,11 @@ Explanation: `342 + 465 = 807`
4747

4848
total = val1 + val2 + carry
4949
carry = int(total / 10)
50-
result.next = ListNode(total % 10)
50+
result.next = SinglyLinkedListNode(total % 10)
5151
result = result.next
5252

5353
if carry > 0:
54-
result.next = ListNode(carry)
54+
result.next = SinglyLinkedListNode(carry)
5555

5656
return dummy_node.next
5757
```
@@ -97,7 +97,7 @@ Given the head of a sorted linked list, delete all duplicates such that each ele
9797
Return the linked list sorted as well.
9898

9999
```python
100-
def delete_duplicates(head: Optional[ListNode]) -> Optional[ListNode]:
100+
def delete_duplicates(head: Optional[SinglyLinkedListNode]) -> Optional[SinglyLinkedListNode]:
101101
node = head
102102

103103
while node:
@@ -118,13 +118,13 @@ Solution with slow/fast
118118
Time: O(N), Space: O(1)
119119

120120
```python
121-
def detect_cycle(head: ListNode) -> Optional[ListNode]:
121+
def detect_cycle(head: SinglyLinkedListNode) -> Optional[SinglyLinkedListNode]:
122122
# detect cycle
123123
if not head or not head.next:
124124
return None
125-
slow: ListNode = head
126-
fast: ListNode = head
127-
intersection: Optional[ListNode] = None
125+
slow: SinglyLinkedListNode = head
126+
fast: SinglyLinkedListNode = head
127+
intersection: Optional[SinglyLinkedListNode] = None
128128

129129
while fast and fast.next:
130130
slow = slow.next
@@ -150,8 +150,8 @@ Solution with set
150150
Time: O(N), Space: O(1)
151151

152152
```python
153-
def detect_cycle_with_set(head: ListNode) -> Optional[ListNode]:
154-
visited_nodes: Set[ListNode] = set()
153+
def detect_cycle_with_set(head: SinglyLinkedListNode) -> Optional[SinglyLinkedListNode]:
154+
visited_nodes: Set[SinglyLinkedListNode] = set()
155155

156156
node = head
157157

@@ -177,7 +177,7 @@ Example:
177177
```
178178

179179
```python
180-
def find_middle_node(head: Optional[ListNode]) -> Optional[ListNode]:
180+
def find_middle_node(head: Optional[SinglyLinkedListNode]) -> Optional[SinglyLinkedListNode]:
181181
if not head:
182182
return None
183183
slow = head
@@ -207,12 +207,12 @@ Solution using two pointers:
207207
Time: O(N), Space: O(1)
208208

209209
```python
210-
def get_intersection_node(head_a: ListNode, head_b: ListNode) -> Optional[ListNode]:
210+
def get_intersection_node(head_a: SinglyLinkedListNode, head_b: SinglyLinkedListNode) -> Optional[SinglyLinkedListNode]:
211211
if not head_a or not head_b:
212212
return None
213213

214-
node_a: ListNode = head_a
215-
node_b: ListNode = head_b
214+
node_a: SinglyLinkedListNode = head_a
215+
node_b: SinglyLinkedListNode = head_b
216216

217217
while node_a != node_b:
218218
node_a = node_a.next
@@ -235,10 +235,10 @@ Solution using Set (less optimal)
235235
Time: O(N), Space: O(N)
236236

237237
```python
238-
def get_intersection_node_with_set(head_a: ListNode, head_b: ListNode) -> Optional[ListNode]:
239-
nodes: Set[ListNode] = set()
240-
node_a: ListNode = head_a
241-
node_b: ListNode = head_b
238+
def get_intersection_node_with_set(head_a: SinglyLinkedListNode, head_b: SinglyLinkedListNode) -> Optional[SinglyLinkedListNode]:
239+
nodes: Set[SinglyLinkedListNode] = set()
240+
node_a: SinglyLinkedListNode = head_a
241+
node_b: SinglyLinkedListNode = head_b
242242

243243
while node_a or node_b:
244244
if node_a:
@@ -265,12 +265,12 @@ linked list where tail connects to. If pos is -1, then there is no cycle in the
265265

266266
```python
267267
# Time: O(N + K) -> O(N), Space: O(1)
268-
def has_cycle(head: Optional['ListNode']) -> bool:
268+
def has_cycle(head: Optional['SinglyLinkedListNode']) -> bool:
269269
if not head or not head.next:
270270
return False
271271

272-
slow: ListNode = head
273-
fast: ListNode = head.next
272+
slow: SinglyLinkedListNode = head
273+
fast: SinglyLinkedListNode = head.next
274274
while slow != fast:
275275
if fast is None or fast.next is None:
276276
return False
@@ -283,12 +283,12 @@ Solution with set (less optimal)
283283

284284
```python
285285
# Time: O(N), Space: O(N)
286-
def has_cycle_with_set(head: Optional['ListNode']) -> bool:
286+
def has_cycle_with_set(head: Optional['SinglyLinkedListNode']) -> bool:
287287
if not head or not head.next:
288288
return False
289289

290-
node_set: Set[ListNode] = set()
291-
node: ListNode = head
290+
node_set: Set[SinglyLinkedListNode] = set()
291+
node: SinglyLinkedListNode = head
292292

293293
while node:
294294
if not node or node in node_set:
@@ -305,8 +305,8 @@ You are given an array of k linked-lists lists, each linked-list is sorted in as
305305
Merge all the linked-lists into one sorted linked-list and return it.
306306

307307
```python
308-
def merge_k_lists(lists: Optional[List[ListNode]]) -> Optional[ListNode]:
309-
dummy = ListNode()
308+
def merge_k_lists(lists: Optional[List[SinglyLinkedListNode]]) -> Optional[SinglyLinkedListNode]:
309+
dummy = SinglyLinkedListNode()
310310
current = dummy
311311

312312
lists = [val for val in lists if val]
@@ -343,13 +343,13 @@ Input: `1->2->4`, `1->3->4`
343343
Output: `1->1->2->3->4->4`
344344

345345
```python
346-
def merge_two_lists(head1: Optional[ListNode], head2: Optional[ListNode]) -> Optional[ListNode]:
346+
def merge_two_lists(head1: Optional[SinglyLinkedListNode], head2: Optional[SinglyLinkedListNode]) -> Optional[SinglyLinkedListNode]:
347347
if not head1 and not head2:
348348
return None
349349

350350
node1 = head1
351351
node2 = head2
352-
dummy_node = ListNode(0)
352+
dummy_node = SinglyLinkedListNode(0)
353353
current_node = dummy_node
354354

355355
while node1 and node2:
@@ -384,7 +384,7 @@ Output: `1->3->5->2->4->NULL`
384384
Time: O(N), Space: O(1)
385385

386386
```python
387-
def odd_even_list(head: Optional[ListNode]) -> Optional[ListNode]:
387+
def odd_even_list(head: Optional[SinglyLinkedListNode]) -> Optional[SinglyLinkedListNode]:
388388
if not head:
389389
return None
390390

@@ -419,7 +419,7 @@ Output: `true`
419419
Time: O(N), Space: O(1)
420420

421421
```python
422-
def is_palindrome(head: Optional[ListNode]) -> bool:
422+
def is_palindrome(head: Optional[SinglyLinkedListNode]) -> bool:
423423
if not head:
424424
return False
425425

@@ -457,12 +457,12 @@ Output: `1->2->3->4->5`
457457
Time: O(N), Space: O(1)
458458

459459
```python
460-
def remove_elements(head: Optional[ListNode], val: int) -> Optional[ListNode]:
461-
dummy_node: ListNode = ListNode(0)
460+
def remove_elements(head: Optional[SinglyLinkedListNode], val: int) -> Optional[SinglyLinkedListNode]:
461+
dummy_node: SinglyLinkedListNode = SinglyLinkedListNode(0)
462462
dummy_node.next = head
463463

464-
previous: ListNode = dummy_node
465-
current: Optional[ListNode] = head
464+
previous: SinglyLinkedListNode = dummy_node
465+
current: Optional[SinglyLinkedListNode] = head
466466

467467
while current:
468468
if current.val == val:
@@ -482,11 +482,11 @@ Given a linked list, remove the n-th node from the end of list and return its he
482482
Time: O(N), Space: O(1)
483483

484484
```python
485-
def remove_nth_from_end(head: ListNode, nth: int) -> Optional[ListNode]:
486-
dummy: ListNode = ListNode(0)
485+
def remove_nth_from_end(head: SinglyLinkedListNode, nth: int) -> Optional[SinglyLinkedListNode]:
486+
dummy: SinglyLinkedListNode = SinglyLinkedListNode(0)
487487
dummy.next = head
488-
previous: ListNode = dummy
489-
lead: ListNode = dummy
488+
previous: SinglyLinkedListNode = dummy
489+
lead: SinglyLinkedListNode = dummy
490490

491491
# Move lead forward
492492
for _ in range(nth + 1):
@@ -509,11 +509,11 @@ Reverse a Singly Linked List.
509509
Time: O(N), Space: O(1)
510510

511511
```python
512-
def reverse_linked_list(head: ListNode) -> Optional[ListNode]:
512+
def reverse_linked_list(head: SinglyLinkedListNode) -> Optional[SinglyLinkedListNode]:
513513
if not head:
514514
return None
515-
previous: Optional[ListNode] = None
516-
current: Optional[ListNode] = head
515+
previous: Optional[SinglyLinkedListNode] = None
516+
current: Optional[SinglyLinkedListNode] = head
517517

518518
while current:
519519
temp = current.next
@@ -529,7 +529,7 @@ Solution using recursion
529529
Time: O(N), Space: O(N)
530530

531531
```python
532-
def reverse_linked_list_recursive(head: ListNode) -> Optional[ListNode]:
532+
def reverse_linked_list_recursive(head: SinglyLinkedListNode) -> Optional[SinglyLinkedListNode]:
533533
if not head or not head.next:
534534
return head
535535

@@ -550,7 +550,7 @@ Output: `4->5->1->2->3->NULL`
550550

551551

552552
```python
553-
def rotate_list(head: Optional[ListNode], amount: int) -> Optional[ListNode]:
553+
def rotate_list(head: Optional[SinglyLinkedListNode], amount: int) -> Optional[SinglyLinkedListNode]:
554554
if not head:
555555
return None
556556

0 commit comments

Comments
 (0)