↓Skip to main content
  1. LeetCode/

LeetCode 876: Middle of the Linked List

·2 mins· ·
LeetCode Easy Linked-List Two-Pointers
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-09-20

  • Submissions: Runtime Error ×3 → Accepted ×3 → Time Limit Exceeded → Runtime Error → Wrong Answer → Accepted

Approach
#

The fast pointer moves twice as fast, so when it finishes, slow is halfway (Linked List Template 3). With while fast and fast.next, even lengths take one extra step and slow stops at the second of the two middles, as the problem wants.

My Attempts
#

My first approach stored the values in a list and indexed the middle, getting the index wrong a few times (/ gave a float for even lengths, and odd lengths added an extra 1), and it returned a value rather than a node. Rewriting with two pointers later:

  • one version never moved node inside the loop and read node.next.next directly, failing on [3];
  • one set middle = node.next before stepping two, one step too far, returning [2] for [7, 1, 7, 2] instead of [7, 2].

Solution
#

class Solution:
    def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
        slow = fast = head
        while fast and fast.next:
            slow, fast = slow.next, fast.next.next
        return slow

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • Even lengths stop at the second middle; the loop condition decides which.

Interview Explanation
#

Fast steps two and slow one; when fast hits the end, slow is in the middle, and fast and fast.next stops at the second middle for even lengths. Time O(n).

Related

LeetCode 234: Palindrome Linked List
·2 mins
LeetCode Easy Linked-List Two-Pointers Stack Recursion
LeetCode 234 note: find the middle with fast and slow pointers, reverse the second half, and compare from both ends with O(1) extra space
LeetCode 141: Linked List Cycle
·2 mins
LeetCode Easy Hash-Table Linked-List Two-Pointers Floyds-Cycle-Finding-Algorithm
LeetCode 141 note: Floyd’s cycle detection, fast two steps and slow one; with a cycle they must meet, each advancing from its own position
LeetCode 160: Intersection of Two Linked Lists
·2 mins
LeetCode Easy Hash-Table Linked-List Two-Pointers
LeetCode 160 note: each pointer walks its own list then the other’s; both cover lenA + lenB steps and reach the intersection, or None, together
LeetCode 821: Shortest Distance to a Character
·2 mins
LeetCode Easy Array Two-Pointers String
LeetCode 821 note: one left-to-right pass for the distance to the nearest c on the left, then a right-to-left pass taking the minimum
LeetCode 832: Flipping an Image
·2 mins
LeetCode Easy Array Two-Pointers Bit-Manipulation Matrix
LeetCode 832 note: reverse each row and invert its bits, which two pointers can do at once
LeetCode 844: Backspace String Compare
·2 mins
LeetCode Easy Two-Pointers String Stack Simulation
LeetCode 844 note: simulate backspaces with a stack; for O(1) space, walk from the end and count how many characters each # skips