1019. Next Greater Node In Linked List(链表)

https://leetcode.com/problems/next-greater-node-in-linked-list/、

题目:求每个节点后面比他大的节点

思路:stack中维持递降的顺序,批量求解,时间复杂度: O(n), 空间复杂度O(n)

代码

class Solution {
public:
    vector<int> nextLargerNodes(ListNode* head) {
        stack<int>sa;
        vector<int>ret;
        vector<int>array;
        int id = 0;
        for( ; head != NULL; head = head->next) {
            for (; !sa.empty() && head->val > array[sa.top()]; sa.pop()) {
                ret[sa.top()] = head->val;
            }
            sa.push(id++);
            array.push_back(head->val);
            ret.push_back(0);
        }
        return ret;
    }
};
### LeetCode 链表设计 Java 实现 #### 设计链表的关键要素 在LeetCode上解决涉及链表的设计问题时,熟悉常见的语言结构和范式有助于更好地理解如何构建高效的链表数据结构[^1]。对于Java而言,了解其面向对象特性以及内置类库的支持非常重要。 #### 单向链表的节点定义 为了创建一个单向链表,在Java中可以先定义表示列表节点的内部静态类`Node`: ```java private static class Node { int val; Node next; public Node(int val) { this.val = val; this.next = null; } } ``` #### 完整的双向链表实现 下面是一个完整的双向链表(MyLinkedList)的简化版本,该实现在LeetCode上的题目要求下进行了优化: ```java class MyLinkedList { private Node head; // 头指针 private Node tail; // 尾指针 private int size; // 节点数量 /** Initialize your data structure here. */ public MyLinkedList() { head = new Node(-1); tail = new Node(-1); head.next = tail; tail.prev = head; size = 0; } /** Get the value of the index-th node in the linked list. If the index is invalid, return -1.*/ public int get(int index) { if (index >= size || index < 0){ return -1; }else{ Node cur = getNode(index); return cur.val; } } /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */ public void addAtHead(int val) { Node newNode = new Node(val); newNode.next = head.next; head.next.prev = newNode; head.next = newNode; newNode.prev = head; ++size; } /** Append a node of value val to the last element of the linked list. */ public void addAtTail(int val) { Node newNode = new Node(val); newNode.prev = tail.prev; tail.prev.next = newNode; newNode.next = tail; tail.prev = newNode; ++size; } /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */ public void addAtIndex(int index, int val) { if (index > size) {return;} Node prev = getNode(index-1); Node curr = prev.next; Node newNode = new Node(val); newNode.next = curr; newNode.prev = prev; prev.next = newNode; curr.prev = newNode; ++size; } /** Delete the index-th node in the linked list, if the index is valid. */ public void deleteAtIndex(int index) { if (index >= size || index < 0){return;} Node prev = getNode(index-1); Node curr = prev.next; prev.next = curr.next; curr.next.prev = prev; --size; } private Node getNode(int index){ Node temp = head; while(index-->=0){ temp=temp.next; } return temp; } } // 双向链表中的节点定义 private static class Node { int val; Node next; Node prev; public Node(int val) { this.val = val; this.next = null; this.prev = null; } } ``` 此代码片段展示了如何通过维护头尾哨兵节点来简化边界条件处理,并提供了基本操作如获取、添加和删除节点的方法。这些方法的时间复杂度均为O(n),其中n为当前链表长度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

计算机的小粽子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值