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
Winner of the Linked List Game INPUT Linked List Structure: 2 idx:0 7 idx:1 4 idx:2 9 idx:3 Even-indexed (Even vals) Odd-indexed (Odd vals) Pairs: Pair 1 (2, 7) Pair 2 (4, 9) Input: head = [2, 7, 4, 9] ALGORITHM STEPS 1 Initialize Score Diff scoreDiff = 0 2 Process Pair 1: (2, 7) 7 > 2, Odd wins scoreDiff += 1 --> 1 3 Process Pair 2: (4, 9) 9 > 4, Odd wins scoreDiff += 1 --> 2 4 Determine Winner scoreDiff > 0 means Odd scoreDiff < 0 means Even scoreDiff = 0 means Tie Final Score Diff: scoreDiff = 2 (Odd leads) FINAL RESULT Score Summary EVEN 0 ODD 2 vs WINNER Odd Output: "Odd" Key Insight: Instead of tracking two separate scores, use a single scoreDiff variable. When odd wins, add 1; when even wins, subtract 1. Final result: scoreDiff > 0 means "Odd", scoreDiff < 0 means "Even", scoreDiff = 0 means "Tie". This approach uses O(1) space and O(n) time complexity. TutorialsPoint - Winner of the Linked List Game | Score Difference Tracking Approach
Asked in
Microsoft 15 Amazon 12 Google 8
23.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen