1. 常用操作和常用技巧
1.1 常用操作
创建一个新节点
#include <iostream>
using namespace std;
struct ListNode {
int val; // 节点的值
ListNode* next; // 指向下一个节点的指针
// 构造函数初始化节点
ListNode(int value) : val(value), next(nullptr) {}
};
尾插
void tailInsert(ListNode*& head, int value) {
ListNode* newNode = new ListNode(value); // 创建新节点
if (head == nullptr) { // 如果链表为空,直接让新节点作为头节点
head = newNode;
return;
}
ListNode* temp = head; // 临时指针遍历到链表末尾
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode; // 将新节点连接到链表尾部
}
头插
void headInsert(ListNode*& head, int value) {
ListNode* newNode = new ListNode(value); // 创建新节点
newNode->next = head; // 新节点指向当前的头节点
head = newNode; // 更新头节点为新节点
}
1.2 常用技巧
(1)引入虚拟“头”节点
反转链表
Node* reverseList(Node* head) {
Node* prev = nullptr;
Node* cur = head;
while (cur) {
Node* nextNode = cur->next;
cur->next = prev;
prev = cur;
cur = nextNode;
}
return prev;
}
(2)快慢双指针
判断是否是带环链表
bool hasCycle(Node* head) {
Node* slow = head;
Node* fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) return true;
}
return false;
}
找到链表中倒数第n个节点
Node* removeNthFromEnd(Node* head, int n) {
Node* dummy = new Node(0);
dummy->next = head;
Node* slow = dummy;
Node* fast = dummy;
for (int i = 0; i <= n; ++i) fast = fast->next;
while (fast) {
slow = slow->next;
fast = fast->next;
}
slow->next = slow->next->next;
return dummy->next;
}
2. 两数相加(中等)
模拟两数相加的过程即可
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* newnode = new ListNode; //申请一个新链表节点
ListNode* pcur = newnode;
int t = 0; // 记录进位
while(l1 || l2 || t)
{
if(l1 != nullptr) // 加上第一个链表的值
{
t += l1->val;
l1 = l1->next;
}
if(l2) //加上第二个链表
{
t += l2->val;
l2 = l2->next;
}
pcur->next = new ListNode(t % 10); // 个位
t /= 10; //得到进位
pcur = pcur->next;
}
pcur = newnode->next;
delete newnode;
return pcur;
}
};
3. 两两交换链表中的节点(中等)
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if(head == nullptr || head->next == nullptr) return head;
ListNode* newnode = new ListNode(0, head);
ListNode* prev = newnode, *cur = head, *next = cur->next, *nnext = next->next;
while(cur && next)
{
//交换节点
prev->next = next;
cur->next = nnext;
next->next = cur;
//修改节点
prev = cur, cur = nnext;
if(cur) next = cur->next;
if(next) nnext = next->next;
}
return newnode->next;
}
};
4. 重排链表(中等)
下面的思路是将先反转后一半链表,然后前半后半链表接替相加即可。
class Solution {
public:
void reorderList(ListNode* head) {
if(head == nullptr || head->next == nullptr || head->next->next == nullptr) return;
//1. 快慢指针找到中间节点
ListNode* fast = head, *slow = head;
while(fast && fast->next)
{
slow = slow->next;
fast = fast->next->next;
}
//2. 反转后半部分链表 头插法
ListNode* head2 = new ListNode(0);
ListNode* cur = slow->next, *next = cur->next;
slow->next = nullptr;
while(cur)
{
next = cur->next;
cur->next = head2->next;
head2->next = cur;
cur = next;
}
//3. 重新排列
ListNode* ret = new ListNode(0);
ListNode* cur1 = head, *cur2 = head2->next;
ListNode* prev = ret;
while(cur1)
{
//先放第一个链表
prev->next = cur1;
cur1 = cur1->next;
prev = prev->next;
//再放第二个链表
if(cur2)
{
prev->next = cur2;
cur2 = cur2->next;
prev = prev->next;
}
}
}
};
5. 合并k个升序链表(困难)
class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
//1. 先计算要翻转多少组链表
ListNode* ccur = head;
int n = 0;
while(ccur)
{
ccur = ccur->next;
n++;
}
n /= k;
//2. 重复n次:长度为k的链表的逆序
ListNode* newnode = new ListNode(0);
ListNode* prev = newnode, *tmp = head;
ListNode *cur = head, *next = cur->next;
for(int i = 0; i < n; ++i)
{
tmp = cur;
for(int j = 0; j < k; ++j)
{
next = cur->next;
cur->next = prev->next;
prev->next = cur;
cur = next;
}
prev = tmp;
}
prev->next = cur;
cur = newnode->next;
delete newnode;
return cur;
}
};