From 4b9d1922eb5813ed299ab3ec591ea047be64ff2f Mon Sep 17 00:00:00 2001 From: Cool <747682928@qq.com> Date: Tue, 26 Nov 2024 15:34:35 +0800 Subject: [PATCH] =?UTF-8?q?2024/11/26=20=E7=81=B5=E8=8C=B6=E9=A2=98?= =?UTF-8?q?=E5=8D=95=20=E5=8D=95=E8=B0=83=E6=A0=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/cool/hot100/linkedlist/ListNode.java | 4 +-- .../cool/ling_cha_mount/stack/Num1019.java | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/cool/ling_cha_mount/stack/Num1019.java diff --git a/src/main/java/com/cool/hot100/linkedlist/ListNode.java b/src/main/java/com/cool/hot100/linkedlist/ListNode.java index 04647fb..94f8b60 100644 --- a/src/main/java/com/cool/hot100/linkedlist/ListNode.java +++ b/src/main/java/com/cool/hot100/linkedlist/ListNode.java @@ -2,8 +2,8 @@ package com.cool.hot100.linkedlist; public class ListNode { - int val; - ListNode next; + public int val; + public ListNode next; ListNode() { } diff --git a/src/main/java/com/cool/ling_cha_mount/stack/Num1019.java b/src/main/java/com/cool/ling_cha_mount/stack/Num1019.java new file mode 100644 index 0000000..15083c2 --- /dev/null +++ b/src/main/java/com/cool/ling_cha_mount/stack/Num1019.java @@ -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 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; + } +}