删除链表元素两题-listnode

82. 删除排序链表中的重复元素 II - 力扣(LeetCode)

要哨兵位:记录头部位置,最后返回哨兵位的下一位就行了;

删除就是cur->next 指向next->next中间跳过一个;

/**
 * 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* deleteDuplicates(ListNode* head) {
        ListNode *dummy = new ListNode(0, head);
        ListNode *cur = dummy;

        while(cur->next != nullptr && cur->next->next != nullptr){
            if(cur->next->val == cur->next->next->val){
                int x = cur->next->val;

                while(cur->next && cur->next->val == x){
                    cur->next = cur->next->next;
                }
            }
            else{
                cur = cur->next;
            }

        }

        return dummy->next;
    }
};

19. 删除链表的倒数第 N 个结点 - 力扣(LeetCode)

哨兵位,phead;

栈先进后出,pop到第n个位置的前一个,把pre->next = pre->next ->next就好了;

最后的移除的节点要delete,以及哨兵位,letcode给的答案移除链表都没delete掉;

/**
 * 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* removeNthFromEnd(ListNode* head, int n) {
        ListNode* phead = new ListNode(0,head);
        stack<ListNode*> stk;

        ListNode* cur = phead;
        while(cur){
            stk.push(cur);
            cur = cur->next;
        }

        for(int i = 1; i <= n; i++){
            stk.pop();
        }

        ListNode* pre = stk.top();

        ListNode* needdelete = pre->next;
        pre->next = pre->next->next;
        ListNode* rs = phead->next;
        delete needdelete;
        delete phead;
        return rs;
    }
};

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这道题目需要我们实现一个循环链表,并在其中删除指定位置的节点。具体实现可以分为以下几个步骤: 1. 定义循环链表的节点结构体,包括数据域和指向下一个节点的指针域。 2. 定义循环链表的头节点,并初始化为空。 3. 读入输入的数据,将其插入到循环链表的尾部。 4. 遍历循环链表,找到需要删除的节点的前一个节点。 5. 将需要删除的节点从链表中摘除,并释放其内存空间。 6. 遍历循环链表,输出剩余节点的数据。 下面是具体的代码实现: ``` #include <iostream> using namespace std; // 定义循环链表的节点结构体 struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(NULL) {} }; int main() { // 定义循环链表的头节点 ListNode* head = NULL; // 读入输入的数据,将其插入到循环链表的尾部 int num; while (cin >> num && num != 0) { ListNode* node = new ListNode(num); if (head == NULL) { head = node; head->next = head; } else { ListNode* cur = head; while (cur->next != head) { cur = cur->next; } cur->next = node; node->next = head; } } // 遍历循环链表,找到需要删除的节点的前一个节点 ListNode* pre = head; while (pre->next != head) { pre = pre->next; } // 将需要删除的节点从链表中摘除,并释放其内存空间 ListNode* cur = head; while (cur->next != head) { if (cur->val % 2 == 0) { pre->next = cur->next; ListNode* temp = cur; cur = cur->next; delete temp; } else { pre = cur; cur = cur->next; } } if (cur->val % 2 == 0) { pre->next = cur->next; delete cur; } // 遍历循环链表,输出剩余节点的数据 cur = head; while (cur->next != head) { cout << cur->val << "\t"; cur = cur->next; } cout << cur->val << endl; return 0; } --相关问题--:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值