Middle of Linked List

Given the head of a singly linked list, return the middle node of the list. If there are two middle nodes (even length), return the second one. Example: Input: head = [1,2,3,4,5] Output: [3] Explanation: 3 is the middle node. Example: Input: head = [1,2,3,4,5,6] Output: [4] Explanation: Two middles (3 and 4), return second one (4). Pattern focus: Fast and Slow Pointers.

Input Format

head = ListNode

Output Format

return ListNode

Constraints

  • The number of nodes is in the range [1, 100].
  • 0 <= Node.val <= 100

Examples

Example 1:

Input:

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

Output:

[3,4,5]

Explanation:

List has 5 nodes; middle is 3 (third node).

Example 2:

Input:

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

Output:

[4,5,6]

Explanation:

Even length (6), return 4 (second middle).

Loading...
Middle of Linked List - Linked List DSA Problem