EasyLinked ListTwo Pointers

Middle of the Linked List

LeetCode
1 approach, code in all languages

You are given the head of a singly linked list. Your job is to find and return the middle node of that list.

When the list contains an odd number of nodes there is a single, unambiguous middle. When the number of nodes is even there are two nodes in the center, and in that case you should return the second of the two.

Example 1

Input: head = [1,2,3,4,5]

Output: [3,4,5]

The list has five nodes, so the middle is the node with value 3.

Example 2

Input: head = [1,2,3,4,5,6]

Output: [4,5,6]

With six nodes the two central values are 3 and 4; we return the second one, the node with value 4.

Constraints

  • The number of nodes is in the range [1, 100].
  • 1 <= Node.val <= 100
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now