LeetCode 3062: Winner of the Linked List Game Solution

Master LeetCode problem 3062 (Winner of the Linked List Game), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

3062. Winner of the Linked List Game

Problem Explanation

Explanation:

To solve this problem, we can iterate through the linked list and find the maximum value node. Then we iterate again to find the position of the maximum value node. If the position of the maximum value node is even, Alice wins; otherwise, Bob wins.

  • We iterate through the linked list to find the maximum value and its position.
  • If the position is even, Alice wins; otherwise, Bob wins.

Time Complexity: O(N) where N is the number of nodes in the linked list. Space Complexity: O(1)

:

Solution Code

class ListNode {
    int val;
    ListNode next;
    ListNode(int val) {
        this.val = val;
    }
}

class Solution {
    public int findWinner(ListNode head) {
        int maxVal = Integer.MIN_VALUE;
        int maxPos = -1;
        int pos = 0;
        
        ListNode curr = head;
        
        while (curr != null) {
            if (curr.val > maxVal) {
                maxVal = curr.val;
                maxPos = pos;
            }
            curr = curr.next;
            pos++;
        }
        
        return maxPos % 2 == 0 ? 1 : 2;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3062 (Winner of the Linked List Game)?

This page provides optimized solutions for LeetCode problem 3062 (Winner of the Linked List Game) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 3062 (Winner of the Linked List Game)?

The time complexity for LeetCode 3062 (Winner of the Linked List Game) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3062 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3062 in Java, C++, or Python.

Back to LeetCode Solutions