Compare commits

...

19 Commits

Author SHA1 Message Date
Hardik Pawar
1ac8c0b6aa
Merge ed62d33870 into f3f32ae3ca 2024-11-21 10:19:54 +05:30
pre-commit-ci[bot]
f3f32ae3ca
[pre-commit.ci] pre-commit autoupdate (#12385)
updates:
- [github.com/astral-sh/ruff-pre-commit: v0.7.3 → v0.7.4](https://github.com/astral-sh/ruff-pre-commit/compare/v0.7.3...v0.7.4)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-11-18 22:07:12 +01:00
Christian Clauss
e3bd7721c8
validate_filenames.py Shebang python for Windows (#12371) 2024-11-15 14:59:14 +01:00
Hardik Pawar
ed62d33870 Fix 2024-10-03 11:56:04 +05:30
Hardik Pawar
1ac1f0c0e4 Fix doctests 2024-10-03 11:54:07 +05:30
Hardik Pawar
fb13521086 Fix 2024-10-03 11:45:03 +05:30
Hardik Pawar
d2b3c1d11a Fix 2024-10-03 11:43:51 +05:30
Hardik Pawar
62d2412482 Fix 2024-10-03 11:40:56 +05:30
Hardik Pawar
4aa953e400 Fix 2024-10-03 11:39:31 +05:30
Hardik Pawar
5556acb7f8 Fix 2024-10-03 11:37:38 +05:30
Hardik Pawar
6211693833 Fix 2024-10-03 11:35:54 +05:30
Hardik Pawar
81d2d511d8 Fix 2024-10-03 11:34:21 +05:30
Hardik Pawar
b3304eb0fd Fix type hint 2024-10-03 11:33:17 +05:30
Hardik Pawar
42a1f27126 Add return type hint 2024-10-03 11:32:07 +05:30
Hardik Pawar
4c53ee9d75 Fix 2024-10-03 11:31:05 +05:30
Hardik Pawar
53d22e577d Merge branch 'merge_sort_linked_list_new_algo' of https://github.com/Hardvan/Python into merge_sort_linked_list_new_algo 2024-10-03 11:28:04 +05:30
Hardik Pawar
809e74b7f2 Fix comments 2024-10-03 11:28:01 +05:30
Hardvan
7ee4af8329 updating DIRECTORY.md 2024-10-02 10:19:43 +00:00
Hardik Pawar
121bfdae5f Add Merge Sort Linked List algorithm 2024-10-02 15:49:25 +05:30
4 changed files with 158 additions and 2 deletions

View File

@ -16,7 +16,7 @@ repos:
- id: auto-walrus
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.3
rev: v0.7.4
hooks:
- id: ruff
- id: ruff-format

View File

@ -265,6 +265,7 @@
* [From Sequence](data_structures/linked_list/from_sequence.py)
* [Has Loop](data_structures/linked_list/has_loop.py)
* [Is Palindrome](data_structures/linked_list/is_palindrome.py)
* [Merge Sort Linked List](data_structures/linked_list/merge_sort_linked_list.py)
* [Merge Two Lists](data_structures/linked_list/merge_two_lists.py)
* [Middle Element Of Linked List](data_structures/linked_list/middle_element_of_linked_list.py)
* [Print Reverse](data_structures/linked_list/print_reverse.py)

View File

@ -0,0 +1,155 @@
class Node:
"""
A class representing a node in a linked list.
Attributes:
data (int): The data stored in the node.
next (Node | None): A reference to the next node in the linked list.
"""
def __init__(self, data: int) -> None:
self.data = data
self.next: Node | None = None
def get_middle(head: Node | None) -> Node | None:
"""
Find the node before the middle of the linked list
using the slow and fast pointer technique.
Parameters:
head: The head node of the linked list.
Returns:
The node before the middle of the linked list,
or None if the list has fewer than 2 nodes.
Example:
>>> head = Node(1)
>>> head.next = Node(2)
>>> head.next.next = Node(3)
>>> middle = get_middle(head)
>>> middle.data
2
"""
if head is None or head.next is None:
return None
slow: Node | None = head
fast: Node | None = head.next
while fast is not None and fast.next is not None:
if slow is None:
return None
slow = slow.next
fast = fast.next.next
return slow
def print_linked_list(head: Node | None) -> None:
"""
Print the linked list in a single line.
Parameters:
head: The head node of the linked list.
Example:
>>> head = Node(1)
>>> head.next = Node(2)
>>> head.next.next = Node(3)
>>> print_linked_list(head)
1 2 3
"""
current = head
first = True # To avoid printing space before the first element
while current:
if not first:
print(" ", end="")
print(current.data, end="")
first = False
current = current.next
print()
def merge(left: Node | None, right: Node | None) -> Node | None:
"""
Merge two sorted linked lists into one sorted linked list.
Parameters:
left: The head of the first sorted linked list.
right: The head of the second sorted linked list.
Returns:
The head of the merged sorted linked list.
Example:
>>> left = Node(1)
>>> left.next = Node(3)
>>> right = Node(2)
>>> right.next = Node(4)
>>> merged = merge(left, right)
>>> print_linked_list(merged)
1 2 3 4
"""
if left is None:
return right
if right is None:
return left
if left.data <= right.data:
result = left
result.next = merge(left.next, right)
else:
result = right
result.next = merge(left, right.next)
return result
def merge_sort_linked_list(head: Node | None) -> Node | None:
"""
Sort a linked list using the Merge Sort algorithm.
Parameters:
head: The head node of the linked list to be sorted.
Returns:
The head node of the sorted linked list.
Example:
>>> head = Node(4)
>>> head.next = Node(2)
>>> head.next.next = Node(1)
>>> head.next.next.next = Node(3)
>>> sorted_head = merge_sort_linked_list(head)
>>> print_linked_list(sorted_head)
1 2 3 4
"""
# Base Case: 0 or 1 node
if head is None or head.next is None:
return head
# Split the linked list into two halves
middle = get_middle(head)
if middle is None or middle.next is None:
return head
next_to_middle = middle.next
middle.next = None # Split the list into two parts
# Recursively sort both halves
left = merge_sort_linked_list(head)
right = merge_sort_linked_list(next_to_middle)
# Merge sorted halves
return merge(left, right)
if __name__ == "__main__":
import doctest
doctest.testmod()

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python3
#!python
import os
try: