Skip to main content

Winner of the Linked List Game - Solution & Explanation

EasyPremiumFree on FleetCodeLinked List8 min read
Practice this problem

Problem Statement

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, e.g., the nodes with indices 0 and 1 are a pair, the nodes with indices 2 and 3 are a pair, and so on.

For every pair, we compare the values of the nodes in the pair:

  • If the odd-indexed node is higher, the "Odd" team gets a point.
  • If the even-indexed node is higher, the "Even" team gets a point.

Return the name of the team with the higher points, if the points are equal, return "Tie".

 

Example 1:

Input: head = [2,1]

Output: "Even"

Explanation: There is only one pair in this linked list and that is (2,1). Since 2 > 1, the Even team gets the point.

Hence, the answer would be "Even".

Example 2:

Input: head = [2,5,4,7,20,5]

Output: "Odd"

Explanation: There are 3 pairs in this linked list. Let's investigate each pair individually:

(2,5) -> Since 2 < 5, The Odd team gets the point.

(4,7) -> Since 4 < 7, The Odd team gets the point.

(20,5) -> Since 20 > 5, The Even team gets the point.

The Odd team earned 2 points while the Even team got 1 point and the Odd team has the higher points.

Hence, the answer would be "Odd".

Example 3:

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

Output: "Tie"

Explanation: There are 2 pairs in this linked list. Let's investigate each pair individually:

(4,5) -> Since 4 < 5, the Odd team gets the point.

(2,1) -> Since 2 > 1, the Even team gets the point.

Both teams earned 1 point.

Hence, the answer would be "Tie".

 

Constraints:

  • The number of nodes in the list is in the range [2, 100].
  • The number of nodes in the list is even.
  • 1 <= Node.val <= 100
  • The value of each odd-indexed node is odd.
  • The value of each even-indexed node is even.

Approach Overview

Problem Overview: You are given the head of a linked list with an even number of nodes. Nodes are compared in pairs: the first node against the second, the third against the fourth, and so on. If the first node in a pair has a larger value, Alice scores a point. If the second node is larger, Bob scores. After processing the entire list, return the player with the higher score or "Tie" if both scores are equal.

Approach 1: Convert Linked List to Array (Simulation) (Time: O(n), Space: O(n))

A straightforward approach is to first copy all node values into an array. Once the values are in a contiguous structure, iterate through the array in steps of two and compare arr[i] with arr[i+1]. Increment Alice's score if the first value is larger, otherwise increment Bob's score when the second value is larger. This approach simplifies traversal logic because random access is available, but it uses extra memory proportional to the number of nodes.

This method is useful when you prefer simpler indexing logic or when the linked list values are needed for additional processing. However, the extra array allocation makes it less optimal compared to operating directly on the linked list.

Approach 2: Direct Linked List Simulation (Time: O(n), Space: O(1))

The optimal solution processes the list directly without extra storage. Use a pointer to traverse the linked list two nodes at a time. For each pair, compare current.val and current.next.val. If the first value is greater, increment Alice's score; if the second is greater, increment Bob's score. Then advance the pointer by two nodes and repeat until the list ends.

This works because the game rules naturally align with pairwise traversal. Each iteration performs constant work: a comparison, a score update, and a pointer jump. The entire list is scanned exactly once, giving O(n) time complexity while maintaining O(1) auxiliary space.

This is essentially a simple simulation over a linked structure. The main detail to watch is pointer movement—always advance by two nodes so each pair is processed exactly once.

Recommended for interviews: The direct linked list simulation is the expected solution. It demonstrates that you can traverse a linked structure efficiently without unnecessary memory allocations. Mentioning the array-based simulation first can show basic reasoning, but implementing the single-pass pointer traversal proves stronger understanding of linked list manipulation and space optimization.

Solution

Traverse the linked list, each time taking out two nodes, compare their values, and then update the scores of odd and even numbers based on the comparison results. Finally, compare the scores of odd and even numbers and return the result.

The time complexity is O(n), where n is the length of the linked list. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Array Conversion + Pair ComparisonO(n)O(n)When simpler indexing logic is preferred or values must be reused later
Direct Linked List SimulationO(n)O(1)Best general solution; single pass with constant extra memory

Video Solution

3062. Winner of the Linked List Game - Week 1/5 Leetcode March Challenge • Programming Live with Larry • 229 views views

Frequently Asked Questions

Is Winner of the Linked List Game easy or hard?
Winner of the Linked List Game is considered an Easy problem. It mainly tests basic linked list traversal and simple simulation logic. The key idea is recognizing that nodes should be processed in pairs while keeping constant extra space.
Winner of the Linked List Game Python/Java solution
The implementation is straightforward in Python, Java, C++, Go, or TypeScript. Maintain two counters for Alice and Bob, traverse the linked list two nodes at a time, compare their values, update scores, and return the player with the higher score or "Tie" if both scores are equal.
How to solve Winner of the Linked List Game in O(n)?
Traverse the linked list while comparing nodes in pairs. For each iteration, compare current.val and current.next.val, update the score for Alice or Bob, and then move the pointer forward by two nodes. This single-pass simulation ensures O(n) time and O(1) extra space.
What is the best approach for Winner of the Linked List Game?
The best approach is a direct simulation of the linked list using a pointer that processes two nodes at a time. Compare the values of each pair and update Alice or Bob's score accordingly. This solution runs in O(n) time and uses O(1) extra space since it does not require converting the list into another data structure.
Is Winner of the Linked List Game asked at Google/Amazon/Meta?
Winner of the Linked List Game is categorized as an easy linked list simulation problem on LeetCode. While it is not commonly reported as a direct interview question at Google, Amazon, or Meta, similar linked list traversal and pair-processing problems frequently appear in technical interviews.
What data structure is used in Winner of the Linked List Game?
The problem uses a singly linked list. The algorithm relies on pointer traversal to move through nodes and compare values pairwise. No additional complex data structures are necessary for the optimal solution.
What is the time complexity of Winner of the Linked List Game?
The optimal solution runs in O(n) time where n is the number of nodes in the linked list. Each node is visited exactly once while processing pairs, and only constant work is done per pair comparison. The space complexity is O(1) because no additional data structures are required.

Ready to solve this problem?

Practice Winner of the Linked List Game with our built-in code editor and test cases.

Practice on FleetCode