EasyLinked ListTwo Pointers

1 approach, code in every language

Problem description

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

After the pattern

Now solve one you haven't seen.

Pump Chain Midpoint is our own problem on the technique behind Middle of the Linked List: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.

1,194 problems · 15,062 graded tests · free