LeetCode - Blind 75 - Linked List Cycle

The Problem Given head, the head of a linked list, determine if the linked list has a cycle in it. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that the tail’s next pointer is connected to. Note that pos is not passed as a parameter. Return true if there is a cycle in the linked list. Otherwise, return false. ...

December 26, 2024 · 2 min · Dmytro Chumakov

LeetCode - Blind 75 - Remove Nth Node From End of List

The Problem Given the head of a linked list, remove the nth node from the end of the list and return its head. Examples Input: head = [1,2,3,4,5], n = 2 Output: [1,2,3,5] Input: head = [1], n = 1 Output: [] Input: head = [1,2], n = 1 Output: [1] Constraints The number of nodes in the list is sz. 1 <= sz <= 30 0 <= Node.val <= 100 1 <= n <= sz Brute Force Solution func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? { var nodes: [ListNode] = [] var curr = head while curr != nil { nodes.append(curr!) curr = curr!.next } let removeIndex = nodes.count - n if removeIndex == 0 { return head?.next } nodes[removeIndex - 1].next = nodes[removeIndex].next return head } Explanation One way to solve this problem is to use extra memory by iterating over all elements in head and storing them in an array. The brute force solution allows us to know the index of each element and delete the node. ...

December 25, 2024 · 3 min · Dmytro Chumakov

LeetCode - Blind 75 - Reorder List

The Problem You are given the head of a singly linked list. The list can be represented as: L0 → L1 → … → Ln - 1 → Ln Reorder the list to be in the following form: L0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → … You may not modify the values in the list’s nodes. Only the nodes themselves may be changed. ...

December 23, 2024 · 3 min · Dmytro Chumakov

LeetCode - Blind 75 - Merge Two Sorted Lists

The Problem You are given the heads of two sorted linked lists, list1 and list2. Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists. Return the head of the merged linked list. Examples Input: list1 = [1,2,4], list2 = [1,3,4] Output: [1,1,2,3,4,4] Input: list1 = [], list2 = [] Output: [] Input: list1 = [], list2 = [0] Output: [0] Constraints The number of nodes in both lists is in the range [0, 50]. -100 <= Node.val <= 100 Both list1 and list2 are sorted in non-decreasing order. Recursive Solution func mergeTwoLists(_ list1: ListNode?, _ list2: ListNode?) -> ListNode? { guard let list1 = list1 else { return list2 } guard let list2 = list2 else { return list1 } if list1.val <= list2.val { list1.next = mergeTwoLists(list1.next, list2) return list1 } else { list2.next = mergeTwoLists(list1, list2.next) return list2 } } Explanation Before moving to the part where we use recursion, we need to handle the base case and check list1 and list2 for nil values. Then we compare the node values and move the next pointer accordingly. ...

December 20, 2024 · 2 min · Dmytro Chumakov

LeetCode - Blind 75 - Reverse Linked List

The problem Given the head of a singly linked list, reverse the list, and return the reversed list. Examples Input: head = [1,2,3,4,5] Output: [5,4,3,2,1] Input: head = [1,2] Output: [2,1] Input: head = [] Output: [] Constraints The number of nodes in the list is in the range [0, 5000]. -5000 <= Node.val <= 5000 Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both? ...

December 17, 2024 · 2 min · Dmytro Chumakov