Skip to the code
cf.
+10 XP

#230 Middle of the Carousel

easy

A product carousel keeps its cards in a linked list and should open centered on the middle card. The list doesn't store its length.

Write middleCard(head) that returns the middle node (the returned list is the middle card and every card after it). When the list has an even number of cards there are two middle cards: return the second one.

Example 1

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

The middle of five cards is the third, 3.

Example 2

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

3 and 4 are both in the middle, so return the second, 4.

Constraints

  • The list has 1 to 100 nodes.
  • 1 <= node.val <= 100

Lists are written as arrays here, like [1, 2, 3]. Your function works with ListNode objects, each with val and next.

Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).