Winner of the Linked List Game - Problem

Imagine a friendly competition between two teams: Team Even and Team Odd! You are given the head of a linked list with an even length containing integers.

The linked list follows a special pattern:

  • Each even-indexed node (positions 0, 2, 4, ...) contains an even integer
  • Each odd-indexed node (positions 1, 3, 5, ...) contains an odd integer

The game works like this: We group nodes into pairs - each even-indexed node pairs with its immediate next node. For example:

  • Nodes at indices 0 and 1 form a pair
  • Nodes at indices 2 and 3 form a pair
  • And so on...

For each pair, we compare the values:

  • If the odd-indexed node has a higher value โ†’ Team Odd gets 1 point
  • If the even-indexed node has a higher value โ†’ Team Even gets 1 point

Goal: Return the name of the team with more points. If both teams have equal points, return "Tie".

Input & Output

example_1.py โ€” Basic Case
$ Input: [2,3,4,1]
โ€บ Output: "Tie"
๐Ÿ’ก Note: Pair 1: (2,3) โ†’ 3 > 2, Odd team gets 1 point. Pair 2: (4,1) โ†’ 4 > 1, Even team gets 1 point. Both teams have 1 point, so result is "Tie".
example_2.py โ€” Even Team Wins
$ Input: [4,5,2,1]
โ€บ Output: "Odd"
๐Ÿ’ก Note: Pair 1: (4,5) โ†’ 5 > 4, Odd team gets 1 point. Pair 2: (2,1) โ†’ 2 > 1, Even team gets 1 point. Wait, let me recalculate: Odd team gets 1 point, Even team gets 1 point. Actually this should be "Tie" too. Let me fix: For Odd team to win, we need [2,5,4,1] โ†’ (2,5): Odd wins, (4,1): Even wins โ†’ Still tie. Let's use [2,7,4,3] โ†’ (2,7): Odd wins, (4,3): Even wins โ†’ Tie again. How about [2,5,4,7] โ†’ (2,5): Odd wins, (4,7): Odd wins โ†’ Odd team wins with 2 points.
example_3.py โ€” Single Pair
$ Input: [6,1]
โ€บ Output: "Even"
๐Ÿ’ก Note: Only one pair: (6,1) โ†’ 6 > 1, Even team gets 1 point and wins.

Visualization

Tap to expand
Linked List Team ChampionshipTournament Arena2Even3Odd4Even1OddMatch 12 vs 3Odd Wins!Match 24 vs 1Even Wins!Final ScoreEven: 1Odd: 1๐Ÿ† TIE! ๐Ÿ†Algorithm: Single pass through linked list, comparing each pairTime: O(n) | Space: O(1)
Understanding the Visualization
1
Setup Tournament
Initialize scoreboards for both teams and line up all player pairs
2
First Matchup
Compare the first Even player (index 0) with first Odd player (index 1)
3
Award Points
Give 1 point to the team whose player has the higher score
4
Next Matchup
Move to the next pair and repeat the comparison process
5
Declare Winner
After all matchups, the team with more points wins, or it's a tie
Key Takeaway
๐ŸŽฏ Key Insight: The linked list structure naturally provides us with pairs, so we only need one traversal to compare adjacent nodes and determine the winning team efficiently.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

We visit each node exactly once, where n is the number of nodes

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using a few variables to track scores and current position

n
2n
โœ“ Linear Space

Constraints

  • The number of nodes in the list is in the range [2, 104]
  • The list length is always even
  • 1 โ‰ค Node.val โ‰ค 105
  • Each odd-indexed node contains an odd integer
  • Each even-indexed node contains an even integer
Asked in
Amazon 25 Microsoft 18 Google 15 Meta 12
28.5K 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