-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path3062-winner-of-the-linked-list-game.js
More file actions
57 lines (52 loc) · 1.53 KB
/
3062-winner-of-the-linked-list-game.js
File metadata and controls
57 lines (52 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* 3062. Winner of the Linked List Game
* https://leetcode.com/problems/winner-of-the-linked-list-game/
* Difficulty: Easy
*
* 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".
*/
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {string}
*/
var gameResult = function(head) {
let evenScore = 0;
let oddScore = 0;
let currentNode = head;
while (currentNode && currentNode.next) {
const evenValue = currentNode.val;
const oddValue = currentNode.next.val;
if (evenValue > oddValue) {
evenScore++;
} else if (oddValue > evenValue) {
oddScore++;
}
currentNode = currentNode.next.next;
}
if (evenScore > oddScore) {
return 'Even';
} else if (oddScore > evenScore) {
return 'Odd';
} else {
return 'Tie';
}
};