From a2c35c69967db300921ea0f54ff2ba5252262459 Mon Sep 17 00:00:00 2001 From: wazedkhan Date: Thu, 2 Jul 2026 21:49:50 +0600 Subject: [PATCH 1/6] LeetCode #206: Reverse Linked List --- prep/practice/linked_list.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 prep/practice/linked_list.py diff --git a/prep/practice/linked_list.py b/prep/practice/linked_list.py new file mode 100644 index 0000000..361c67e --- /dev/null +++ b/prep/practice/linked_list.py @@ -0,0 +1,28 @@ +from typing import Optional + + +class ListNode: + def __init__(self, val=0, next=None): + self.val = val + self.next = next + + +def print_list(head): + current = head + while current is not None: + print(current.val, end=" ") + current = current.next + print() + + +class Solution: + def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: + prev = None + current = head + + while current: + next_node = current.next + current.next = prev + prev = current + current = next_node + return prev From c342d898494d8a070a620951faa638dc194a75f0 Mon Sep 17 00:00:00 2001 From: wazedkhan Date: Fri, 3 Jul 2026 09:26:05 +0600 Subject: [PATCH 2/6] LeetCode #21: Merge Two Sorted Lists --- prep/practice/linked_list.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/prep/practice/linked_list.py b/prep/practice/linked_list.py index 361c67e..66e36d4 100644 --- a/prep/practice/linked_list.py +++ b/prep/practice/linked_list.py @@ -26,3 +26,23 @@ def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: prev = current current = next_node return prev + + def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: + dummy = ListNode() + tail = dummy + + while list1 and list2: + if list1.val < list2.val: + tail.next = list1 + list1 = list1.next + else: + tail.next = list2 + list2 = list2.next + tail = tail.next + + if list1: + tail.next = list1 + else: + tail.next = list2 + + return dummy.next From 01dc7e038f5641f36749eab583ef495d981e5a3c Mon Sep 17 00:00:00 2001 From: wazedkhan Date: Fri, 3 Jul 2026 16:45:42 +0600 Subject: [PATCH 3/6] LeetCode-141: Linked List Cycle(set) --- prep/practice/linked_list.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/prep/practice/linked_list.py b/prep/practice/linked_list.py index 66e36d4..ac2d1af 100644 --- a/prep/practice/linked_list.py +++ b/prep/practice/linked_list.py @@ -46,3 +46,12 @@ def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> tail.next = list2 return dummy.next + + def hasCycle(self, head: Optional[ListNode]) -> bool: + seen = set() + while head: + if head in seen: + return True + seen.add(head) + head = head.next + return False From 05eab5e6a9ef0bbdc7789f1bbd0956fea8fecc8d Mon Sep 17 00:00:00 2001 From: wazedkhan Date: Fri, 3 Jul 2026 16:53:46 +0600 Subject: [PATCH 4/6] =?UTF-8?q?LeetCode-141:=20Linked=20List=20Cycle(Floyd?= =?UTF-8?q?=E2=80=99s=20Tortoise=20and=20Hare=20Algorithm)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- prep/practice/linked_list.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/prep/practice/linked_list.py b/prep/practice/linked_list.py index ac2d1af..fd0a89d 100644 --- a/prep/practice/linked_list.py +++ b/prep/practice/linked_list.py @@ -48,10 +48,12 @@ def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> return dummy.next def hasCycle(self, head: Optional[ListNode]) -> bool: - seen = set() - while head: - if head in seen: + tortoise, hare = head, head + + while hare and hare.next: + tortoise = tortoise.next + hare = hare.next.next + if tortoise == hare: return True - seen.add(head) - head = head.next + return False From faf982267459a9abeb74cebb16acca85383824de Mon Sep 17 00:00:00 2001 From: wazedkhan Date: Fri, 3 Jul 2026 22:07:20 +0600 Subject: [PATCH 5/6] LeetCode-19: Remove Nth Node From End of List --- prep/practice/linked_list.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/prep/practice/linked_list.py b/prep/practice/linked_list.py index fd0a89d..0b457c3 100644 --- a/prep/practice/linked_list.py +++ b/prep/practice/linked_list.py @@ -57,3 +57,22 @@ def hasCycle(self, head: Optional[ListNode]) -> bool: return True return False + + def list_len(self, head: Optional[ListNode]) -> int: + current = head + count = 0 + while current: + count += 1 + current = current.next + return count + + def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: + pos = self.list_len(head) - n + dummy = ListNode(0) + dummy.next = head + current = dummy + + for _ in range(pos): + current = current.next + current.next = current.next.next + return dummy.next From e1c7e2327dd57a3e6488de357f338b5cc2a202fe Mon Sep 17 00:00:00 2001 From: wazedkhan Date: Sat, 4 Jul 2026 11:26:08 +0600 Subject: [PATCH 6/6] LeetCode-143: Reorder List --- prep/practice/linked_list.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/prep/practice/linked_list.py b/prep/practice/linked_list.py index 0b457c3..18a176a 100644 --- a/prep/practice/linked_list.py +++ b/prep/practice/linked_list.py @@ -76,3 +76,28 @@ def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNod current = current.next current.next = current.next.next return dummy.next + + def reorderList(self, head: Optional[ListNode]) -> None: + slow, fast = head, head + + while fast and fast.next: + slow = slow.next + fast = fast.next.next + half = slow.next + slow.next = None + + prev = None + while half: + next_node = half.next + half.next = prev + prev = half + half = next_node + + while head and prev: + nxt_head = head.next + head.next = prev + nxt_prev = prev.next + prev.next = nxt_head + + head = nxt_head + prev = nxt_prev