![](https://i-blog.csdnimg.cn/direct/765701cf9e7a463598a0168c495427d7.png)
![](https://i-blog.csdnimg.cn/direct/01f89ba03e0a404db8810a6a86a2d814.png)
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
vector<int> res;
ListNode* cur = head;
while(cur != nullptr){
res.push_back(cur->val);
cur = cur->next;
}
reverse(res.begin(), res.end()); //注意反转的用法!!!
return res;
}
};
class Solution { //递归法
public:
vector<int> res;
vector<int> printListFromTailToHead(ListNode* head) {
if(head == nullptr) return res;
printListFromTailToHead(head->next);
res.push_back(head->val);
return res;
}
};
![](https://i-blog.csdnimg.cn/direct/2bab247c4e254f69ae972c43dafc38be.png)
class Solution {
public:
ListNode* ReverseList(ListNode* head) {
ListNode *pre = nullptr, *cur = head, *tmp = head->next;
while(cur != nullptr){
cur->next = pre;
pre = cur; cur = tmp;
if(tmp != nullptr) tmp = tmp->next;
}
return pre;
}
};
![](https://i-blog.csdnimg.cn/direct/99366eb29236401ba34349ddfd24ba88.png)
class Solution {
public:
ListNode* Merge(ListNode* pHead1, ListNode* pHead2) {
// write code here
ListNode* dummy = new ListNode(0);
dummy->next = pHead1; ListNode* pre = dummy;
ListNode *cur1 = pHead1, *cur2 = pHead2;
while(cur1 != nullptr && cur2 != nullptr){
// 比较 p1 和 p2 两个指针,将值较小的的节点接到 p 指针
if(cur1->val <= cur2->val) { //错误写成了cur1 <= cur2!!!
pre->next = cur1;
cur1 = cur1->next;
}
else{
pre->next = cur2;
cur2 = cur2->next;
}
pre = pre->next;
}
if(cur1 != nullptr) pre->next = cur1;
if(cur2 != nullptr) pre->next = cur2;
ListNode* res = dummy->next;
delete dummy; dummy = nullptr;
return res;
}
};
![](https://i-blog.csdnimg.cn/direct/6528da2e2b284d36b7ebda1f327dd336.png)
class Solution {
public:
ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
unordered_set<ListNode*> uset;
ListNode* cur = pHead1;
while(cur != nullptr) {uset.insert(cur); cur = cur->next;}
cur = pHead2;
while(cur != nullptr){
if( uset.find(cur) !=uset.end() ) return cur;
else cur = cur->next;
}
return nullptr;
}
};
![](https://i-blog.csdnimg.cn/direct/9eed945985a3480ba2cbc28ea678efba.png)
class Solution {
public:
ListNode* EntryNodeOfLoop(ListNode* pHead) {
ListNode *slow = pHead, *fast = pHead;
while(fast != nullptr && fast->next != nullptr){
slow = slow->next;
fast = fast->next->next;
if(slow == fast) break;
}
if(fast == nullptr || fast->next == nullptr) return nullptr;
slow = pHead;
while(slow != fast) {slow = slow->next; fast = fast->next;}
return slow;
}
};
![](https://i-blog.csdnimg.cn/direct/4510a39f28d54a939085c77f540aa8d6.png)
class Solution {
public:
ListNode* FindKthToTail(ListNode* pHead, int k) {
ListNode *slow = pHead, *fast = pHead;
while(k && fast != nullptr) {fast = fast->next; k--;} //--操作不能放在 while(k--)!!!
if(fast == nullptr && k != 0) return nullptr;
while(fast != nullptr) {slow = slow->next; fast = fast->next;}
return slow;
}
};
![](https://i-blog.csdnimg.cn/direct/d3b309020eba4dad88651c416729e172.png)
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) { // 删除链表中重复的元素
if (head == NULL) return NULL; // 如果链表为空,直接返回NULL
ListNode *slow = head, *fast = head; // 定义快慢指针,初始都指向头结点
while (fast != NULL) { // 只要快指针没有遍历完整个链表
if (fast->val != slow->val) { // 快慢指针值不同
slow->next = fast; // 慢指针连接新节点
slow = slow->next; // 慢指针向后移动一位
}
fast = fast->next; // 快指针向后移动一位
}
slow->next = NULL; // 断开与后面重复元素的连接
return head; // 返回头结点
}
};
![](https://i-blog.csdnimg.cn/direct/73a5fc13705947c89a60543c651b23d3.png)
class Solution {
public:
ListNode* deleteDuplication(ListNode* pHead) {
ListNode* dummy = new ListNode(0);
dummy->next = pHead;
ListNode * pre = dummy, * cur = pHead;
while(cur != nullptr && cur->next != nullptr){
if(cur->val == cur->next->val) {
int temp = cur->val;
while(cur != nullptr && cur->val == temp) cur = cur->next;
pre->next = cur;
}
else { //不重复时pre才能向前移,故而得分情况讨论
pre->next = cur;
pre = pre->next;
cur = cur->next;
}
}
ListNode* res = dummy->next;
delete dummy; dummy = nullptr;
return res;
}
};
![](https://i-blog.csdnimg.cn/direct/2570a1187f7845069cf2d0e02fdfe136.png)
class Solution {
public:
ListNode* deleteNode(ListNode* head, int val) {
ListNode * dummy = new ListNode(0);
dummy->next = head;
ListNode *pre = dummy;
while(pre->next != nullptr){
if(pre->next->val == val) {
ListNode* temp = pre->next;
pre->next = pre->next->next;
delete temp;
}
else pre = pre->next;
}
ListNode* res = dummy->next;
delete dummy; dummy = nullptr;
return res;
}
};