1019:链表中的下一个更大节点

This commit is contained in:
huangge1199 2021-06-07 11:20:28 +08:00
parent 8dfe5d64e9
commit 602b60c36d
2 changed files with 132 additions and 0 deletions

View File

@ -0,0 +1,95 @@
//给出一个以头节点 head 作为第一个节点的链表链表中的节点分别编号为node_1, node_2, node_3, ...
//
// 每个节点都可能有下一个更大值next larger value对于 node_i如果其 next_larger(node_i) node_j.
//val那么就有 j > i node_j.val > node_i.val j 是可能的选项中最小的那个如果不存在这样的 j那么下一个更大值为 0
//
//
// 返回整数答案数组 answer其中 answer[i] = next_larger(node_{i+1})
//
// 注意在下面的示例中诸如 [2,1,5] 这样的输入不是输出是链表的序列化表示其头节点的值为 2第二个节点值为 1第三个节点值为 5
//
//
//
// 示例 1
//
// 输入[2,1,5]
//输出[5,5,0]
//
//
// 示例 2
//
// 输入[2,7,4,3,5]
//输出[7,0,5,5,0]
//
//
// 示例 3
//
// 输入[1,7,5,1,9,2,5,1]
//输出[7,9,9,9,0,5,0,0]
//
//
//
//
// 提示
//
//
// 对于链表中的每个节点1 <= node.val <= 10^9
// 给定列表的长度在 [0, 10000] 范围内
//
// Related Topics 链表
// 👍 156 👎 0
package leetcode.editor.cn;
import com.code.leet.entiy.ListNode;
import java.util.ArrayList;
import java.util.List;
//1019:链表中的下一个更大节点
public class NextGreaterNodeInLinkedList{
public static void main(String[] args) {
//测试代码
Solution solution = new NextGreaterNodeInLinkedList().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public int[] nextLargerNodes(ListNode head) {
List<Integer> list = new ArrayList<>();
List<Integer> result = new ArrayList<>();
List<Integer> indexs = new ArrayList<>();
int max = 0;
int index =0;
while (head != null) {
indexs.add(index);
if (head.val > max) {
int size = indexs.size();
for (int i = 0; i < size; i++) {
if(head.val>result.get(indexs.get(i))){
result.set(indexs.get(i),head.val);
indexs.remove(indexs.get(i));
}
}
max = head.val;
}
index++;
result.add(0);
list.add(head.val);
head = head.next;
}
result.add(0);
return result.stream().mapToInt(Integer::valueOf).toArray();
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,37 @@
<p>给出一个以头节点&nbsp;<code>head</code>&nbsp;作为第一个节点的链表。链表中的节点分别编号为:<code>node_1, node_2, node_3, ...</code></p>
<p>每个节点都可能有下一个更大值(<em>next larger</em> <strong>value</strong>):对于&nbsp;<code>node_i</code>,如果其&nbsp;<code>next_larger(node_i)</code>&nbsp;&nbsp;<code>node_j.val</code>,那么就有&nbsp;<code>j &gt; i</code>&nbsp;&nbsp;&nbsp;<code>node_j.val &gt; node_i.val</code>,而&nbsp;<code>j</code>&nbsp;是可能的选项中最小的那个。如果不存在这样的&nbsp;<code>j</code>,那么下一个更大值为&nbsp;<code>0</code>&nbsp;</p>
<p>返回整数答案数组&nbsp;<code>answer</code>,其中&nbsp;<code>answer[i] = next_larger(node_{i+1})</code>&nbsp;</p>
<p><strong><em>注意:</em></strong>在下面的示例中,诸如 <code>[2,1,5]</code> 这样的<strong>输入</strong>(不是输出)是链表的序列化表示,其头节点的值为&nbsp;2第二个节点值为 1第三个节点值为&nbsp;5 。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>[2,1,5]
<strong>输出:</strong>[5,5,0]
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>[2,7,4,3,5]
<strong>输出:</strong>[7,0,5,5,0]
</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>[1,7,5,1,9,2,5,1]
<strong>输出:</strong>[7,9,9,9,0,5,0,0]
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ol>
<li>对于链表中的每个节点,<code>1 &lt;= node.val&nbsp;&lt;= 10^9</code></li>
<li>给定列表的长度在 <code>[0, 10000]</code>&nbsp;范围内</li>
</ol>
<div><div>Related Topics</div><div><li></li><li>链表</li></div></div>\n<div><li>👍 156</li><li>👎 0</li></div>