Skip to content

Commit 2b39851

Browse files
committed
feat: add solutions to lc problem: No.1019. Next Greater Node in Linked List
1 parent d53a554 commit 2b39851

File tree

9 files changed

+220
-93
lines changed

9 files changed

+220
-93
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
## 介绍
1616

17-
本项目包含 [LeetCode](https://leetcode-cn.com/problemset/all/)[《剑指 Offer(第 2 版)》](https://leetcode-cn.com/problemset/lcof/)[《程序员面试金典(第 6 版)》](https://leetcode-cn.com/problemset/lcci/)等题目的相关题解。所有题解均由多种编程语言实现,包括但不限于:Java、Python、C++、JavaScript、C#、Go,日常更新。欢迎 Star 关注本项目「[GitHub](https://github.com/doocs/leetcode) / [Gitee](https://gitee.com/doocs/leetcode)」,获取项目最新动态。
17+
本项目包含 [LeetCode](https://leetcode-cn.com/problemset/all/)[《剑指 Offer(第 2 版)》](https://leetcode-cn.com/problemset/lcof/)[《程序员面试金典(第 6 版)》](https://leetcode-cn.com/problemset/lcci/)等题目的相关题解。所有题解均由多种编程语言实现,包括但不限于:Java、Python、C++、JavaScript、C#、Go,日常更新。欢迎 Star 🌟 关注本项目「[GitHub](https://github.com/doocs/leetcode) / [Gitee](https://gitee.com/doocs/leetcode)」,获取项目最新动态。
1818

1919
[English Version](/README_EN.md)
2020

solution/0800-0899/0876.Middle of the Linked List/README.md

+33-3
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,57 @@ ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, 以及 ans.next.next.next
3838
<li>给定链表的结点数介于 <code>1</code> 和 <code>100</code> 之间。</li>
3939
</ul>
4040

41-
4241
## 解法
4342

4443
<!-- 这里可写通用的实现逻辑 -->
4544

45+
“快慢指针”实现。
46+
4647
<!-- tabs:start -->
4748

4849
### **Python3**
4950

5051
<!-- 这里可写当前语言的特殊实现逻辑 -->
5152

5253
```python
53-
54+
# Definition for singly-linked list.
55+
# class ListNode:
56+
# def __init__(self, val=0, next=None):
57+
# self.val = val
58+
# self.next = next
59+
class Solution:
60+
def middleNode(self, head: ListNode) -> ListNode:
61+
slow = fast = head
62+
while fast and fast.next:
63+
slow, fast = slow.next, fast.next.next
64+
return slow
5465
```
5566

5667
### **Java**
5768

5869
<!-- 这里可写当前语言的特殊实现逻辑 -->
5970

6071
```java
61-
72+
/**
73+
* Definition for singly-linked list.
74+
* public class ListNode {
75+
* int val;
76+
* ListNode next;
77+
* ListNode() {}
78+
* ListNode(int val) { this.val = val; }
79+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
80+
* }
81+
*/
82+
class Solution {
83+
public ListNode middleNode(ListNode head) {
84+
ListNode slow = head, fast = head;
85+
while (fast != null && fast.next != null) {
86+
slow = slow.next;
87+
fast = fast.next.next;
88+
}
89+
return slow;
90+
}
91+
}
6292
```
6393

6494
### **...**

solution/0800-0899/0876.Middle of the Linked List/README_EN.md

+31-22
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,14 @@
66

77
<p>Given a non-empty, singly&nbsp;linked list with head node <code>head</code>, return&nbsp;a&nbsp;middle node of linked list.</p>
88

9-
10-
119
<p>If there are two middle nodes, return the second middle node.</p>
1210

13-
14-
1511
<p>&nbsp;</p>
1612

17-
18-
1913
<div>
2014

2115
<p><strong>Example 1:</strong></p>
2216

23-
24-
2517
<pre>
2618

2719
<strong>Input: </strong><span id="example-input-1-1">[1,2,3,4,5]</span>
@@ -36,14 +28,10 @@ ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, and ans.next.next.next = N
3628

3729
</pre>
3830

39-
40-
4131
<div>
4232

4333
<p><strong>Example 2:</strong></p>
4434

45-
46-
4735
<pre>
4836

4937
<strong>Input: </strong><span id="example-input-2-1">[1,2,3,4,5,6]</span>
@@ -54,16 +42,10 @@ Since the list has two middle nodes with values 3 and 4, we return the second on
5442

5543
</pre>
5644

57-
58-
5945
<p>&nbsp;</p>
6046

61-
62-
6347
<p><strong>Note:</strong></p>
6448

65-
66-
6749
<ul>
6850
<li>The number of nodes in the given list will be between <code>1</code>&nbsp;and <code>100</code>.</li>
6951
</ul>
@@ -72,22 +54,49 @@ Since the list has two middle nodes with values 3 and 4, we return the second on
7254

7355
</div>
7456

75-
76-
7757
## Solutions
7858

7959
<!-- tabs:start -->
8060

8161
### **Python3**
8262

8363
```python
84-
64+
# Definition for singly-linked list.
65+
# class ListNode:
66+
# def __init__(self, val=0, next=None):
67+
# self.val = val
68+
# self.next = next
69+
class Solution:
70+
def middleNode(self, head: ListNode) -> ListNode:
71+
slow = fast = head
72+
while fast and fast.next:
73+
slow, fast = slow.next, fast.next.next
74+
return slow
8575
```
8676

8777
### **Java**
8878

8979
```java
90-
80+
/**
81+
* Definition for singly-linked list.
82+
* public class ListNode {
83+
* int val;
84+
* ListNode next;
85+
* ListNode() {}
86+
* ListNode(int val) { this.val = val; }
87+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
88+
* }
89+
*/
90+
class Solution {
91+
public ListNode middleNode(ListNode head) {
92+
ListNode slow = head, fast = head;
93+
while (fast != null && fast.next != null) {
94+
slow = slow.next;
95+
fast = fast.next.next;
96+
}
97+
return slow;
98+
}
99+
}
91100
```
92101

93102
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1-
public ListNode middleNode(ListNode head) {
2-
ListNode low = head, first = head;
3-
while (first != null && first.next != null) {
4-
low = low.next;
5-
first = first.next.next;
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
public ListNode middleNode(ListNode head) {
13+
ListNode slow = head, fast = head;
14+
while (fast != null && fast.next != null) {
15+
slow = slow.next;
16+
fast = fast.next.next;
17+
}
18+
return slow;
619
}
7-
return low;
820
}
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
11
# Definition for singly-linked list.
22
# class ListNode:
3-
# def __init__(self, x):
4-
# self.val = x
5-
# self.next = None
6-
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
76
class Solution:
8-
def middleNode(self, head):
9-
"""
10-
:type head: ListNode
11-
:rtype: ListNode
12-
"""
13-
if not head:
14-
return None
15-
if not head.next:
16-
return head
17-
fast=head
18-
slow=head
19-
while fast.next:
20-
fast=fast.next.next
21-
slow=slow.next
22-
if not fast or not fast.next:
23-
return slow
7+
def middleNode(self, head: ListNode) -> ListNode:
8+
slow = fast = head
9+
while fast and fast.next:
10+
slow, fast = slow.next, fast.next.next
11+
return slow

solution/1000-1099/1019.Next Greater Node In Linked List/README.md

+47-3
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,71 @@
4343
<li>给定列表的长度在 <code>[0, 10000]</code>&nbsp;范围内</li>
4444
</ol>
4545

46-
4746
## 解法
4847

4948
<!-- 这里可写通用的实现逻辑 -->
5049

50+
“单调栈”实现。
51+
5152
<!-- tabs:start -->
5253

5354
### **Python3**
5455

5556
<!-- 这里可写当前语言的特殊实现逻辑 -->
5657

5758
```python
58-
59+
# Definition for singly-linked list.
60+
# class ListNode:
61+
# def __init__(self, x):
62+
# self.val = x
63+
# self.next = None
64+
65+
class Solution:
66+
def nextLargerNodes(self, head: ListNode) -> List[int]:
67+
nums = []
68+
while head:
69+
nums.append(head.val)
70+
head = head.next
71+
s = []
72+
larger = [0] * len(nums)
73+
for i, num in enumerate(nums):
74+
while s and nums[s[-1]] < num:
75+
larger[s.pop()] = num
76+
s.append(i)
77+
return larger
5978
```
6079

6180
### **Java**
6281

6382
<!-- 这里可写当前语言的特殊实现逻辑 -->
6483

6584
```java
66-
85+
/**
86+
* Definition for singly-linked list.
87+
* public class ListNode {
88+
* int val;
89+
* ListNode next;
90+
* ListNode(int x) { val = x; }
91+
* }
92+
*/
93+
class Solution {
94+
public int[] nextLargerNodes(ListNode head) {
95+
List<Integer> nums = new ArrayList<>();
96+
while (head != null) {
97+
nums.add(head.val);
98+
head = head.next;
99+
}
100+
Deque<Integer> s = new ArrayDeque<>();
101+
int[] larger = new int[nums.size()];
102+
for (int i = 0; i < nums.size(); ++i) {
103+
while (!s.isEmpty() && nums.get(s.peek()) < nums.get(i)) {
104+
larger[s.pop()] = nums.get(i);
105+
}
106+
s.push(i);
107+
}
108+
return larger;
109+
}
110+
}
67111
```
68112

69113
### **...**

0 commit comments

Comments
 (0)