• 1019 链表的下一个更大节点(单调栈)


    题目

    链表的下一个更大节点

    给定一个长度为 n 的链表 head

    对于列表中的每个节点,查找下一个 更大节点 的值。也就是说,对于每个节点,找到它旁边的第一个节点的值,这个节点的值 严格大于 它的值。

    返回一个整数数组 answer ,其中 answer[i] 是第 i 个节点( 从1开始 )的下一个更大的节点的值。如果第 i 个节点没有下一个更大的节点,设置 answer[i] = 0 。

    示例 1:

    输入:head = [2,1,5]
    输出:[5,5,0]
    

    示例 2:

    输入:head = [2,7,4,3,5]
    输出:[7,0,5,5,0]
    
    

    提示:

    • 链表中节点数为 n
    • 1 <= n <= 104
    • 1 <= Node.val <= 109

    题解

    从右向左

    寻找下一个更大元素

    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. private int n;
    13. public int[] nextLargerNodes(ListNode head) {
    14. //从右向左 查找下一个更大元素
    15. head = reverse(head);
    16. int[] ans = new int[n];
    17. Deque st = new ArrayDeque<>();
    18. for (ListNode cur = head; cur != null; cur = cur.next) {
    19. while (!st.isEmpty() && st.peek() <= cur.val) {
    20. st.pop();
    21. }
    22. ans[--n] = st.isEmpty() ? 0 : st.peek();
    23. st.push(cur.val);
    24. }
    25. return ans;
    26. }
    27. public ListNode reverse(ListNode head) {
    28. ListNode pre = null;
    29. ListNode cur = head;
    30. while (cur != null) {
    31. ListNode nxt = cur.next;
    32. cur.next = pre;
    33. pre = cur;
    34. cur = nxt;
    35. n++;
    36. }
    37. return pre;
    38. }
    39. }

    也可以使用递归倒序遍历

    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. private int[] ans;
    13. private final Deque st = new ArrayDeque<>();
    14. public int[] nextLargerNodes(ListNode head) {
    15. f(head, 0);
    16. return ans;
    17. }
    18. private void f(ListNode head, int i) {
    19. if (head == null) {
    20. ans = new int[i];
    21. return;
    22. }
    23. f(head.next, i + 1);
    24. while (!st.isEmpty() && st.peek() <= head.val) {
    25. st.pop();
    26. }
    27. if (!st.isEmpty()) {
    28. ans[i] = st.peek();
    29. }
    30. st.push(head.val);
    31. }
    32. }

    从左向右

    更新下一个更大元素

    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 int[] nextLargerNodes(ListNode head) {
    13. int n = 0;
    14. for (ListNode cur = head; cur != null; cur = cur.next) {
    15. n++;
    16. }
    17. int[] ans = new int[n];
    18. Deque<int[]> st = new ArrayDeque<>();// 保存节点值和节点下标
    19. int i = 0;
    20. for (ListNode cur = head; cur != null; cur = cur.next) {
    21. while (!st.isEmpty() && st.peek()[0] < cur.val) {
    22. ans[st.pop()[1]] = cur.val;// 更新答案
    23. }
    24. st.push(new int[]{cur.val, i++});
    25. }
    26. return ans;
    27. }
    28. }
    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 int[] nextLargerNodes(ListNode head) {
    13. int n = 0;
    14. for (ListNode cur = head; cur != null; cur = cur.next) {
    15. n++;
    16. }
    17. int[] ans = new int[n];
    18. Deque st = new ArrayDeque<>();// 保存节点下标
    19. int i = 0;
    20. for (ListNode cur = head; cur != null; cur = cur.next) {
    21. while (!st.isEmpty() && ans[st.peek()] < cur.val) {
    22. ans[st.pop()] = cur.val;// 更新答案
    23. }
    24. st.push(i);
    25. ans[i++] = cur.val;
    26. }
    27. for (var index : st) {
    28. ans[index] = 0;
    29. }
    30. return ans;
    31. }
    32. }

  • 相关阅读:
    关于汽车E\E架构演进的思考(2)
    【C之switch】使用及技巧
    云原生数据湖应用洞察白皮书
    解决module ‘os‘ has no attribute ‘add_dll_directory‘
    我的NeRF学习——初步认识
    一文拿捏对象内存布局及JMM(JAVA内存模型)
    Vue-cli全局配置整理
    洗车小程序源码:10个必备功能,提升洗车体验
    SpringCloud整合OpenFeign
    C/C++计算邮资 2019年9月电子学会青少年软件编程(C/C++)等级考试一级真题答案解析
  • 原文地址:https://blog.csdn.net/fffffall/article/details/134260175