diff --git a/prep/practice/linked_list.py b/prep/practice/linked_list.py new file mode 100644 index 0000000..18a176a --- /dev/null +++ b/prep/practice/linked_list.py @@ -0,0 +1,103 @@ +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 + + 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 + + def hasCycle(self, head: Optional[ListNode]) -> bool: + tortoise, hare = head, head + + while hare and hare.next: + tortoise = tortoise.next + hare = hare.next.next + if tortoise == hare: + 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 + + 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