//删除指定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
标签:current,head,ListNode,val,--,next,链表,curr,leetcode
From: https://www.cnblogs.com/MR---Zhao/p/18033281