Leetcode 876: Middle of the Linked List

Problem Statement Description Given the head of a singly linked list, return the middle node of the linked list. If there are two middle nodes, return the second middle node. Examples Example 1: flowchart LR a((1))-->b((2))-->c(3)-->d((4))-->e((5)) Input: head = [1,2,3,4,5] Output: [3,4,5] Explanation: The middle node of the list is node 3. Example 2: flowchart LR a((1))-->b((2))-->c((3))-->d(4)-->e((5))-->f((6)) Input: head = [1,2,3,4,5,6] Output: [4,5,6] Explanation: Since the list has two middle nodes with values 3 and 4, we return the second one....

January 17, 2023 · 3 min · 487 words · Me

Leetcode 206: Reverse Linked List

Problem Statement Description Given the head of a singly linked list, reverse the list, and return the reversed list. Examples Example 1: Input: head = [1,2,3,4,5] Output: [5,4,3,2,1] Example 2: Input: head = [1,2] Output: [2,1] Example 3: Input: head = [] Output: [] Constraints The number of nodes in the list is the range [0, 5000] -5000 <= Node.val <= 5000 Key Insights In order to reverse a singly linked list iteratively, you need to use three pointers: prev, curr and next....

January 14, 2023 · 3 min · 430 words · Me

Leetcode 141: Linked List Cycle

Problem Statement 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 tail’s next pointer is connected to. Note that pos is not passed as a parameter....

January 10, 2023 · 2 min · 243 words · Me

Leetcode 21: Merge Two Sorted Lists

Problem Statement You are given the heads of two sorted linked lists list1 and list2. Merge the two lists in a 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. Example 1: """ 1-->2-->4-->5 ^list1 1-->3-->4 ^list2 1-->1-->2-->3-->4-->4-->5 ^curr """ Input: list1 = [1,2,4,5], list2 = [1,3,4] Output: [1,1,2,3,4,4,5] Example 2: Input: list1 = [], list2 = [] Output: [] Example 3: Input: list1 = [], list2 = [0] Output: [0] Solution Intuition & Patterns In order to keep track of the head of the merged list, we will create a dummy node which will function as a pre-head node....

January 10, 2023 · 2 min · 302 words · Me