Winner of the Linked List Game - Problem
You are given the head of a linked list of even length containing integers.
Each odd-indexed node contains an odd integer and each even-indexed node contains an even integer.
We call each even-indexed node and its next node a pair. For example:
- Nodes with indices 0 and 1 are a pair
- Nodes with indices 2 and 3 are a pair
- And so on...
For every pair, we compare the values:
- If the odd-indexed node value is higher →
"Odd"team gets a point - If the even-indexed node value is higher →
"Even"team gets a point
Return the name of the team with the higher points. If the points are equal, return "Tie".
Input & Output
Example 1 — Basic Case
$
Input:
head = [2,7,4,9]
›
Output:
Odd
💡 Note:
Pair 1: (2,7) → 7 > 2, Odd gets 1 point. Pair 2: (4,9) → 9 > 4, Odd gets 1 point. Final score: Even=0, Odd=2, so Odd wins.
Example 2 — Even Team Wins
$
Input:
head = [8,3,6,1]
›
Output:
Even
💡 Note:
Pair 1: (8,3) → 8 > 3, Even gets 1 point. Pair 2: (6,1) → 6 > 1, Even gets 1 point. Final score: Even=2, Odd=0, so Even wins.
Example 3 — Tie Game
$
Input:
head = [10,5,2,7]
›
Output:
Tie
💡 Note:
Pair 1: (10,5) → 10 > 5, Even gets 1 point. Pair 2: (2,7) → 7 > 2, Odd gets 1 point. Final score: Even=1, Odd=1, so it's a Tie.
Constraints
- The linked list has even length
- 2 ≤ length ≤ 100
- Each odd-indexed node contains an odd integer
- Each even-indexed node contains an even integer
- 1 ≤ Node.val ≤ 100
Visualization
Tap to expand
Understanding the Visualization
1
Input
Linked list with alternating even/odd values at even/odd indices
2
Pair Formation
Group adjacent nodes into pairs and compare values
3
Score & Winner
Count wins for each team and determine the winner
Key Takeaway
🎯 Key Insight: Process the linked list in pairs, comparing adjacent nodes to track team scores efficiently in a single pass
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code