ListNode 2487. 从链表中移除节点,单调栈的应用

一、题目

1、题目描述

给你一个链表的头节点 head 。

移除每个右侧有一个更大数值的节点。

返回修改后链表的头节点 head 

2、接口描述

 
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeNodes(ListNode* head) {
        
    }
};

3、原题链接

2487. 从链表中移除节点


二、解题报告

1、思路分析

可见题目要求我们把原链表修改为一个非升序链表,那么我们只需要遍历一遍链表,维护一个单调栈存储节点,保证从栈底到栈顶单调递减,遍历完之后将栈中节点串联起来,栈底节点就是表头

2、复杂度

时间复杂度:O(n) 空间复杂度:O(1)

3、代码详解

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeNodes(ListNode* head) {
        if(!head || !(head -> next))
            return head;
        stack<ListNode*> s;
        s.push(head);
        head = head -> next;
        while(head)
        {
            while(s.size() && head -> val > s.top() -> val)
                s.pop();
            s.push(head);
            head = head -> next;
        }
        while(s.size())
        {
            s.top() -> next = head;
            head = s.top();
            s.pop();
        }        
        return head;
    }
};

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我会为您解答这个问题。 首先,我们需要定义一个链表节点数据结构,包括节点的值和指向下一个节点的指针,代码如下: ```c++ struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(NULL) {} }; ``` 然后,我们需要实现以下四个操作函数: 1. 创建一个空链表。 ```c++ ListNode* createList() { ListNode* head = new ListNode(0); return head; } ``` 这个函数创建了一个空链表,也就是只有一个头节点链表。 2. 在链表的末尾插入一个节点。 ```c++ void insertNode(ListNode* head, int val) { ListNode* newNode = new ListNode(val); ListNode* p = head; while (p->next != NULL) { p = p->next; } p->next = newNode; } ``` 这个函数会创建一个新节点,并将其插入到链表的末尾。 3. 在链表删除指定节点。 ```c++ void deleteNode(ListNode* head, int val) { ListNode* p = head; while (p->next != NULL) { if (p->next->val == val) { ListNode* temp = p->next; p->next = temp->next; delete temp; return; } p = p->next; } } ``` 这个函数会在链表找到值为val的节点,并将其删除。 4. 打印链表的所有节点值。 ```c++ void printList(ListNode* head) { ListNode* p = head->next; while (p != NULL) { cout << p->val << " "; p = p->next; } cout << endl; } ``` 这个函数会遍历整个链表,并打印出每一个节点的值。 完整代码如下: ```c++ #include <iostream> using namespace std; struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(NULL) {} }; ListNode* createList() { ListNode* head = new ListNode(0); return head; } void insertNode(ListNode* head, int val) { ListNode* newNode = new ListNode(val); ListNode* p = head; while (p->next != NULL) { p = p->next; } p->next = newNode; } void deleteNode(ListNode* head, int val) { ListNode* p = head; while (p->next != NULL) { if (p->next->val == val) { ListNode* temp = p->next; p->next = temp->next; delete temp; return; } p = p->next; } } void printList(ListNode* head) { ListNode* p = head->next; while (p != NULL) { cout << p->val << " "; p = p->next; } cout << endl; } int main() { ListNode* head = createList(); insertNode(head, 1); insertNode(head, 2); insertNode(head, 3); insertNode(head, 4); printList(head); deleteNode(head, 3); printList(head); return 0; } ``` 希望能够帮到您!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

EQUINOX1

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

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

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

打赏作者

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

抵扣说明:

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

余额充值