|
| 1 | +# [3062. Winner of the Linked List Game](https://leetcode.cn/problems/winner-of-the-linked-list-game) |
| 2 | + |
| 3 | +[English Version](/solution/3000-3099/3062.Winner%20of%20the%20Linked%20List%20Game/README_EN.md) |
| 4 | + |
| 5 | +<!-- tags: --> |
| 6 | + |
| 7 | +## 题目描述 |
| 8 | + |
| 9 | +<!-- 这里写题目描述 --> |
| 10 | + |
| 11 | +<p>You are given the <code>head</code> of a linked list of <strong>even</strong> length containing integers.</p> |
| 12 | + |
| 13 | +<p>Each <strong>odd-indexed</strong> node contains an odd integer and each <strong>even-indexed</strong> node contains an even integer.</p> |
| 14 | + |
| 15 | +<p>We call each even-indexed node and its next node a <strong>pair</strong>, e.g., the nodes with indices <code>0</code> and <code>1</code> are a pair, the nodes with indices <code>2</code> and <code>3</code> are a pair, and so on.</p> |
| 16 | + |
| 17 | +<p>For every <strong>pair</strong>, we compare the values of the nodes in the pair:</p> |
| 18 | + |
| 19 | +<ul> |
| 20 | + <li>If the odd-indexed node is higher, the <code>"Odd"</code> team gets a point.</li> |
| 21 | + <li>If the even-indexed node is higher, the <code>"Even"</code> team gets a point.</li> |
| 22 | +</ul> |
| 23 | + |
| 24 | +<p>Return <em>the name of the team with the <strong>higher</strong> points, if the points are equal, return</em> <code>"Tie"</code>.</p> |
| 25 | + |
| 26 | +<p> </p> |
| 27 | +<p><strong class="example">Example 1: </strong></p> |
| 28 | + |
| 29 | +<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;"> |
| 30 | +<p><strong>Input: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> head = [2,1] </span></p> |
| 31 | + |
| 32 | +<p><strong>Output: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> "Even" </span></p> |
| 33 | + |
| 34 | +<p><strong>Explanation: </strong> There is only one pair in this linked list and that is <code>(2,1)</code>. Since <code>2 > 1</code>, the Even team gets the point.</p> |
| 35 | + |
| 36 | +<p>Hence, the answer would be <code>"Even"</code>.</p> |
| 37 | +</div> |
| 38 | + |
| 39 | +<p><strong class="example">Example 2: </strong></p> |
| 40 | + |
| 41 | +<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;"> |
| 42 | +<p><strong>Input: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> head = [2,5,4,7,20,5] </span></p> |
| 43 | + |
| 44 | +<p><strong>Output: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> "Odd" </span></p> |
| 45 | + |
| 46 | +<p><strong>Explanation: </strong> There are <code>3</code> pairs in this linked list. Let's investigate each pair individually:</p> |
| 47 | + |
| 48 | +<p><code>(2,5)</code> -> Since <code>2 < 5</code>, The Odd team gets the point.</p> |
| 49 | + |
| 50 | +<p><code>(4,7)</code> -> Since <code>4 < 7</code>, The Odd team gets the point.</p> |
| 51 | + |
| 52 | +<p><code>(20,5)</code> -> Since <code>20 > 5</code>, The Even team gets the point.</p> |
| 53 | + |
| 54 | +<p>The Odd team earned <code>2</code> points while the Even team got <code>1</code> point and the Odd team has the higher points.</p> |
| 55 | + |
| 56 | +<p>Hence, the answer would be <code>"Odd"</code>.</p> |
| 57 | +</div> |
| 58 | + |
| 59 | +<p><strong class="example">Example 3: </strong></p> |
| 60 | + |
| 61 | +<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;"> |
| 62 | +<p><strong>Input: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> head = [4,5,2,1] </span></p> |
| 63 | + |
| 64 | +<p><strong>Output: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> "Tie" </span></p> |
| 65 | + |
| 66 | +<p><strong>Explanation: </strong> There are <code>2</code> pairs in this linked list. Let's investigate each pair individually:</p> |
| 67 | + |
| 68 | +<p><code>(4,5)</code> -> Since <code>4 < 5</code>, the Odd team gets the point.</p> |
| 69 | + |
| 70 | +<p><code>(2,1)</code> -> Since <code>2 > 1</code>, the Even team gets the point.</p> |
| 71 | + |
| 72 | +<p>Both teams earned <code>1</code> point.</p> |
| 73 | + |
| 74 | +<p>Hence, the answer would be <code>"Tie"</code>.</p> |
| 75 | +</div> |
| 76 | + |
| 77 | +<p> </p> |
| 78 | +<p><strong>Constraints:</strong></p> |
| 79 | + |
| 80 | +<ul> |
| 81 | + <li>The number of nodes in the list is in the range <code>[2, 100]</code>.</li> |
| 82 | + <li>The number of nodes in the list is even.</li> |
| 83 | + <li><code>1 <= Node.val <= 100</code></li> |
| 84 | + <li>The value of each odd-indexed node is odd.</li> |
| 85 | + <li>The value of each even-indexed node is even.</li> |
| 86 | +</ul> |
| 87 | + |
| 88 | +## 解法 |
| 89 | + |
| 90 | +### 方法一 |
| 91 | + |
| 92 | +<!-- tabs:start --> |
| 93 | + |
| 94 | +```python |
| 95 | + |
| 96 | +``` |
| 97 | + |
| 98 | +```java |
| 99 | + |
| 100 | +``` |
| 101 | + |
| 102 | +```cpp |
| 103 | + |
| 104 | +``` |
| 105 | + |
| 106 | +```go |
| 107 | + |
| 108 | +``` |
| 109 | + |
| 110 | +<!-- tabs:end --> |
| 111 | + |
| 112 | +<!-- end --> |
0 commit comments