【leetcode】链表篇刷题 --
//删除指定value节点 class Solution { public: ListNode* removeElements(ListNode* head, int val) { //单独处理head while(head != NULL && head->val == val){ ListNode* temp = head; head = head->next; delete temp; } ListNode* current = head; //处理非head while(current != NULL && current->next != NULL){ if(val == current->next->val){ ListNode* temp = current->next; current->next = current->next->next; delete temp; }else{ current = current->next; } } return head; } };
/* * @lc app=leetcode.cn id=203 lang=cpp * * [203] 移除链表元素 */ // @lc code=start /** * 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* removeElements(ListNode* head, int val) { //用于统一做删除处理 ListNode* fHead = new ListNode(0); fHead->next = head; //用于遍历list ListNode* current = fHead; while(current->next != NULL){ if(current->next->val == val){ ListNode* temp = current->next; current->next = current->next->next; delete temp; }else{ current = current->next; } } //返回时特别注意,经删除操作后,list已发生变化,返回虚拟头节点的next即可 head = fHead->next; delete fHead; return head; } }; // @lc code=end
/* * @lc app=leetcode.cn id=206 lang=cpp * * [206] 反转链表 */ // @lc code=start /** * 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* reverseList(ListNode* head) { ListNode* prev = nullptr; ListNode* curr = head; while(curr){ ListNode* next = curr->next; curr->next = prev; prev = curr; curr = next; } //循环结束后curr=null return prev; } }; // @lc code=end
/* * @lc app=leetcode.cn id=206 lang=cpp * * [206] 反转链表 */ // @lc code=start /** * 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* reverseList(ListNode* head) { return reverse(nullptr, head); } ListNode* reverse(ListNode* prev, ListNode* cur){ if(cur == nullptr){ return prev; } ListNode* temp = cur->next; cur->next = prev; //注意这里cur,对应参数prev,temp对应参数cur //相当于将prev与cur分别下移一位 //就等同于实现: prev = cur; cur = temp; return reverse(cur,temp); } }; // @lc code=end
/* * @lc app=leetcode.cn id=24 lang=cpp * * [24] 两两交换链表中的节点 */ // @lc code=start /** * 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* swapPairs(ListNode* head) { ListNode* dummyHand = new ListNode(0); dummyHand->next = head; ListNode* curr = dummyHand; //curr在完成第一对交换后,每次循环均指向上一次交换中的node1节点so 判断是否满足交换条件 //需要含有 curr->next->next != nullptr //而curr->next!=nullptr是为了上面这句判断条件不报错(如果curr->next=null则curr->next->next != nullptr,空指针异常) while(curr->next != nullptr && curr->next->next != nullptr){ ListNode* node1 = curr->next; ListNode* node2 = curr->next->next; curr->next = node2; node1->next = node2->next; node2->next = node1; curr = node1; } return dummyHand->next; } }; // @lc code=end
本文来自博客园,作者:main(void),转载请注明原文链接:https://www.cnblogs.com/MR---Zhao/p/18033281
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理