2024/11/26 灵茶题单 单调栈
This commit is contained in:
parent
5f49f65c77
commit
4b9d1922eb
|
@ -2,8 +2,8 @@ package com.cool.hot100.linkedlist;
|
||||||
|
|
||||||
|
|
||||||
public class ListNode {
|
public class ListNode {
|
||||||
int val;
|
public int val;
|
||||||
ListNode next;
|
public ListNode next;
|
||||||
|
|
||||||
ListNode() {
|
ListNode() {
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
package com.cool.ling_cha_mount.stack;
|
||||||
|
|
||||||
|
import com.cool.hot100.linkedlist.ListNode;
|
||||||
|
|
||||||
|
import java.util.Deque;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
public class Num1019 {
|
||||||
|
public int[] nextLargerNodes(ListNode head) {
|
||||||
|
Deque<int[]> stack = new LinkedList<>();
|
||||||
|
int len = 0;
|
||||||
|
ListNode node = head;
|
||||||
|
while (node != null) {
|
||||||
|
len++;
|
||||||
|
node = node.next;
|
||||||
|
}
|
||||||
|
int[] res = new int[len];
|
||||||
|
node = head;
|
||||||
|
int i = 0;
|
||||||
|
while (node != null) {
|
||||||
|
while (!stack.isEmpty() && stack.peek()[0] < node.val) {
|
||||||
|
res[stack.pop()[1]] = node.val;
|
||||||
|
}
|
||||||
|
stack.push(new int[]{node.val, i++});
|
||||||
|
node = node.next;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue