1.两两交换链表的节点
非常经典的定义虚拟头节点法,这个方法一定要牢记!
代码如下:
class Solution {
public:
ListNode* swapPairs(ListNode* head) { //非常经典!一定要牢记
if (head == nullptr || head->next == nullptr) {
return head;
}
ListNode* dummy = new ListNode(0);
dummy->next = head;
ListNode* prev = dummy;
ListNode* curr = dummy->next;
while (curr && curr->next) {
ListNode* temp = curr->next;
curr->next = temp->next;
temp->next = curr;
prev->next = temp;
prev = curr;
curr = curr->next;
}
return dummy->next;
}
};
可在本地IDE运行的完整代码如下:
#include <iostream>
using namespace std;
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(nullptr) {}
};
class Solution {
public:
ListNode* swapPairs(ListNode* head) { //非常经典!一定要牢记
if (head == nullptr || head->next == nullptr) {
return head;
}
ListNode* dummy = new ListNode(0);
dummy->next = head;
ListNode* prev = dummy;
ListNode* curr = dummy->next;
while (curr && curr->next) {
ListNode* temp = curr->next;
curr->next = temp->next;
temp->next = curr;
prev->next = temp;
prev = curr;
curr = curr->next;
}
return dummy->next;
}
};
void printList(ListNode* head) {
ListNode* curr = head;
while (curr != nullptr) {
cout << curr->val << " ";
curr = curr->next;
}
cout << endl;
}
int main() {
ListNode* head = new ListNode(1);
head->next = new ListNode(2);
head->next->next = new ListNode(6);
head->next->next->next = new ListNode(3);
Solution sol;
ListNode* swapped = sol.swapPairs(head);
printList(swapped);
return 0;
}
2.删除链表的倒数第N个节点
这道题也非常巧妙,主要就是使用两个指针,并定义虚拟头节点。首先,快指针和慢指针都指向dummy虚拟头节点,然后将快指针先移动N+1步,然后快指针和慢指针一起移动,直到快指针指向链表的最后,慢指针就正好指向倒数第N个节点的前面那个节点,就可以方便的将其倒数第N个节点删除。
代码如下:
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) { //简洁版本
ListNode* dummy = new ListNode(0);
dummy->next = head;
ListNode* fast = dummy;
ListNode* slow = dummy;
n++; //fast指针先走n+1步,然后slow指针再跟着走,这样子就可以方便的实现删除倒数第n个指针
while (n-- && fast) {
fast = fast->next;
}
while (fast) {
fast = fast->next;
slow = slow->next;
}
slow->next = slow->next->next;
ListNode* newNode = dummy->next;
delete dummy;
return newNode;
}
};
可在本地IDE运行的完整代码如下:
#include <iostream>
using namespace std;
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(nullptr) {}
};
// class Solution {
// public:
// ListNode* removeNthFromEnd(ListNode* head, int n) { //我自己的版本,是可以跑得出的,但是还是不够简洁,其实还可以写的更简洁
// ListNode* dummy = new ListNode(0);
// ListNode* curr = head;
// dummy->next = head;
// ListNode* prev = dummy;
// ListNode* temp = new ListNode(0);
// int num = 1;
// if (head == nullptr || head->next == nullptr) {
// return nullptr;
// }
// while (curr){
// curr = curr->next;
// if (num >= n) {
// temp = prev;
// prev = prev->next;
// }
// num++;
// }
// temp->next = prev->next;
// return dummy->next;
// }
// };
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) { //简洁版本
ListNode* dummy = new ListNode(0);
dummy->next = head;
ListNode* fast = dummy;
ListNode* slow = dummy;
n++; //fast指针先走n+1步,然后slow指针再跟着走,这样子就可以方便的实现删除倒数第n个指针
while (n-- && fast) {
fast = fast->next;
}
while (fast) {
fast = fast->next;
slow = slow->next;
}
slow->next = slow->next->next;
ListNode* newNode = dummy->next;
delete dummy;
return newNode;
}
};
void printList(ListNode* head) {
ListNode* curr = head;
while (curr){
cout << curr->val << " ";
curr = curr->next;
}
cout << endl;
}
int main() {
ListNode* head = new ListNode(1);
head->next = new ListNode(2);
head->next->next = new ListNode(3);
head->next->next->next = new ListNode(4);
head->next->next->next->next = new ListNode(5);
int n = 2;
Solution sol;
ListNode* ans = sol.removeNthFromEnd(head, n);
printList(ans);
return 0;
}
3.面试题02.07链表相交
这道题的主要思路是先将两个链表的长度求出,然后让长链表先移动,移动到和短链表长度相同的时候,再将两个链表一起移动,直到移动到两个链表相交。
代码如下:
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode* currA = headA;
ListNode* currB = headB;
int numA = 0, numB = 0;
while (currA) {
currA = currA->next;
numA++;
}
while (currB) {
currB = currB->next;
numB++;
}
currA = headA; //重新将其指针currA和currB复位
currB = headB;
if (numA > numB) {
while (numA > numB) {
currA = currA->next;
numA--;
}
} else if (numA < numB) {
while (numA < numB) {
currB = currB->next;
numB--;
}
}
while (currA != currB) {
currA = currA->next;
currB = currB->next;
}
return currA;
}
};
可在本地IDE完整运行的代码如下:
#include <iostream>
using namespace std;
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(nullptr) {}
};
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode* currA = headA;
ListNode* currB = headB;
int numA = 0, numB = 0;
while (currA) {
currA = currA->next;
numA++;
}
while (currB) {
currB = currB->next;
numB++;
}
currA = headA; //重新将其指针currA和currB复位
currB = headB;
if (numA > numB) {
while (numA > numB) {
currA = currA->next;
numA--;
}
} else if (numA < numB) {
while (numA < numB) {
currB = currB->next;
numB--;
}
}
while (currA != currB) {
currA = currA->next;
currB = currB->next;
}
return currA;
}
};
void printList(ListNode* head) {
ListNode* curr = head;
while (curr) {
cout << curr->val << " ";
curr = curr->next;
}
cout << endl;
}
int main() {
Solution sol;
ListNode* headA = new ListNode(4);
headA->next = new ListNode(1);
headA->next->next = new ListNode(8);
headA->next->next->next = new ListNode(4);
headA->next->next->next->next = new ListNode(5);
ListNode* headB = new ListNode(5);
headB->next = new ListNode(0);
headB->next->next = new ListNode(1);
headB->next->next->next = new ListNode(8);
headB->next->next->next->next = new ListNode(4);
headB->next->next->next->next->next = new ListNode(5);
ListNode* ans = sol.getIntersectionNode(headA, headB);
printList(ans);
return 0;
}
4.环形链表
这道题我个人认为有一定难度。其中,有两个难点:1.如何知道链表是否相交 2.如何找到链表相交的入口点。
关于第一个问题,可以定义两个指针,一个快指针和一个慢指针。快指针一次走两步,慢指针一次走一步。如果快指针和慢指针能够相遇,那么则说明该链表中有环!
关于第二个问题,需要一些数学公式的推导,Carl的代码随想录中有详细的推导公式和动图,可以去查看:代码随想录:环形链表
代码如下:
class Solution {
public:
ListNode *detectCycle(ListNode *head) { //首先,如何判断链表是否有环?定义两个指针fast和slow,快指针每次移动两格,慢指针每次移动一格。如果有环的话,快慢指针会相遇。
//其次,如果有环,如何找到环的起始点?这其中设计一些数学推导,推导十分醍醐灌顶,最好去代码随想录中阅读!
ListNode* fast = head;
ListNode* slow = head;
while (fast && fast->next) {
fast = fast->next->next;
slow = slow->next;
if (fast == slow) {
ListNode* index1 = head;
ListNode* index2 = fast;
while (index1 != index2) {
index1 = index1->next;
index2 = index2->next;
}
return index1;
}
}
return nullptr;
}
};
可在本地IDE完整运行的代码如下:
#include <iostream>
using namespace std;
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(nullptr) {}
};
class Solution {
public:
ListNode *detectCycle(ListNode *head) { //首先,如何判断链表是否有环?定义两个指针fast和slow,快指针每次移动两格,慢指针每次移动一格。如果有环的话,快慢指针会相遇。
//其次,如果有环,如何找到环的起始点?这其中设计一些数学推导,推导十分醍醐灌顶,最好去代码随想录中阅读!
ListNode* fast = head;
ListNode* slow = head;
while (fast && fast->next) {
fast = fast->next->next;
slow = slow->next;
if (fast == slow) {
ListNode* index1 = head;
ListNode* index2 = fast;
while (index1 != index2) {
index1 = index1->next;
index2 = index2->next;
}
return index1;
}
}
return nullptr;
}
};
void printList(ListNode* head) {
ListNode* curr = head;
while (curr) {
cout << curr->val << " ";
curr = curr->next;
}
cout << endl;
}
int main() {
Solution sol;
// 创建一个带有环的链表
ListNode* head = new ListNode(1);
ListNode* cycleNode = new ListNode(2);
head->next = cycleNode;
cycleNode->next = new ListNode(3);
cycleNode->next->next = new ListNode(4);
cycleNode->next->next->next = cycleNode; // 创建循环
// 调用 detectCycle 函数查找环的起始点
ListNode* cycleStart = sol.detectCycle(head);
if (cycleStart) {
cout << "链表有环,环的起始点的值为: " << cycleStart->val << endl;
} else {
cout << "链表没有环" << endl;
}
return 0;
}
今毕。