这里写目录标题
1、翻转链表
在遍历链表时,将当前节点的next 指针改为指向前一个节点。由于节点没有引用其前一个节点,因此必须事先存储其前一个节点。在更改引用之前,还需要存储后一个节点。最后返回新的头引用。
头插法:
![在这里插入图片描述](https://img-blog.csdnimg.cn/30b36c6eb2944221bbe884342dc731ff.jpg?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAcXFfMTI0ODc0MjQ2Nw==,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)
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;
}
return prev;
}
};
2、删除单链表中倒数第k个节点
快慢指针的思想。我们将第一个指针fast 指向链表的第k+1 个节点,第二个指针 slow 指向链表的第一个节点,此时指针 fast 与slow 二者之间刚好间隔 k 个节点。此时两个指针同步向后走,当第一个指针fast 走到链表的尾部空节点时,则此时slow 指针刚好指向链表的倒数第k个节点。
class Solution {
public:
ListNode* getKthFromEnd(ListNode* head, int k) {
ListNode* fast = head;
ListNode* slow = head;
while (fast && k > 0) {
fast = fast->next;
k--;
}
while (fast) {
fast = fast->next;
slow = slow->next;
}
return slow;
}
};
3、如何判断一个链表有环
定义一个快指针和一个慢指针,慢指针一次走一步,快指针一次走两步。如果快指针追上了慢指针,则链表有环;如果快指针走到末尾也没追上慢指针,则无环。
bool IsLoop(node *head)
{
if (head == NULL)
{
return false;
}
node *slow = head->next;
if (slow == NULL)
{
return false;
}
node *fast = slow->next;
while (fast != NULL && slow != NULL)
{
if (fast == slow)
{
return true;
}
slow = slow->next;
fast = fast->next;
if (fast != NULL)
{
fast = fast->next;
}
}
return false;
}
4、打印两个有序链表的公共部分
首先让两个序列的头指针止住对应有序链表的头部,然后比较这两个指针所指元素的大小,谁的值小,对应序列的指针就后移,然后继续比较,谁小谁后移。若两者元素大小相等,则打印输出,之后同时后移。直到有一个序列的指针越界后,程序停止。
void print_common_part(node *head1, node *head2) {
while(head1 != NULL && head2 != NULL) {
if(head1 -> value < head2 -> value)
head1 = head1 -> next;
else if(head1 -> value > head2 -> value)
head2 = head2 -> next;
else {
cout << head1 -> value << ' ';
head1 = head1 -> next;
head2 = head2 -> next;
}
}
}
5、删除链表的中间节点
输入:节点 5 (位于单向链表 4->5->1->9 中)
输出:不返回任何数据,从链表中删除传入的节点 5,使链表变为 4->1->9
class Solution {
public:
void deleteNode(ListNode* node) {
node->val = node->next->val;
node->next = node->next->next;
}
};
6、如何判断两个链表是否相交
A长度为 a, B长度为b, 假设存在交叉点,此时 A到交叉点距离为 c, 而B到交叉点距离为d
后续交叉后长度是一样的,那么就是 a-c = b-d -> a+d = b+c
这里意味着只要分别让A和B额外多走一遍B和A,那么必然会走到交叉,注意这里边缘情况是,大家都走到null依然没交叉,那么正好返回null即可
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode* a = headA;
ListNode* b = headB;
while (a != b)
{
a = a != nullptr ? a->next : headB;
b = b != nullptr ? b->next : headA;
}
return a;
}
};
7、合并两个排序的链表
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
// 如果链表1为空链表就返回链表2; 如果链表1为空链表就返回链表1
if (l1 == nullptr) return l2;
else if (l2 == nullptr) return l1;
// 定义一个新的合并链表链表
ListNode* pMergeHead = nullptr;
// 如果链表1的val小于链表2的val, 就把链表1的节点赋给合并链表的当前节点, 然后进入递归过程
// 链表1的val大于或等于链表2的val, 就把链表2的节点赋给合并链表的当前节点, 然后进入递归
if (l1->val < l2->val) {
pMergeHead = l1;
pMergeHead->next = mergeTwoLists(l1->next, l2);
} else {
pMergeHead = l2;
pMergeHead->next = mergeTwoLists(l1, l2->next);
}
// 返回合并链表
return pMergeHead;
}
};
8、复杂的链表复制
请实现 copyRandomList 函数,复制一个复杂链表。在复杂链表中,每个节点除了有一个 next 指针指向下一个节点,还有一个 random 指针指向链表中的任意节点或者 null。
第一次遍历,将链表复制多一半
第二次遍历,复制random
第三次遍历,将链表拆分为原链表和新的复制链表
时间O(n),空间O(1)
class Solution {
public:
Node* copyRandomList(Node* head) {
if (head == nullptr) return nullptr;
for (Node* node = head; node; node = node->next->next) {
Node* nodeNew = new Node(node->val);
nodeNew->next = node->next;
node->next = nodeNew;
}
for (Node* node = head; node; node = node->next->next) {
Node* nodeNew = node->next;
nodeNew->random = node->random ? node->random->next : nullptr;
}
Node* headNew = head->next;
for (Node* node = head; node; node = node->next) {
Node* nodeNew = node->next;
node->next = node->next->next;
nodeNew->next = nodeNew->next? nodeNew->next->next : nullptr;
}
return headNew;
}
};
9、两个链表的第一个公共节点
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if (headA == nullptr || headB == nullptr) {
return nullptr;
}
ListNode *pA = headA, *pB = headB;
while (pA != pB) {
//若是pa遍历完了,则从pB的头开始走,反之亦然
pA = pA == nullptr ? headB : pA->next;
pB = pB == nullptr ? headA : pB->next;
}
return pA;
}
};
10、链表中环的入口
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode *fast = head, *slow = head;
while (true) {
if (fast == nullptr || fast->next == nullptr) return nullptr;
fast = fast->next->next;
slow = slow->next;
if (fast == slow) break;
}
//上面就是确定有环,并且fast和slow都在环内
fast = head;
while (slow != fast) {
slow = slow->next;
fast = fast->next;
}
return fast;
}
};
11、链表中环的大小
int GetLoopLength(Node* head)
{
Node *slow = head, *fast = head;
//判断有环
while ( fast && fast->next )
{
slow = slow->next;
fast = fast->next->next;
if ( slow == fast )//第一次相遇
break;
}
slow = slow->next;
fast = fast->next->next;
int length = 1; //环长度
while ( fast != slow )//再次相遇
{
slow = slow->next;
fast = fast->next->next;
length ++; //累加
}
return length;
}
12、删除链表中的重复元素
具体地,我们从指针 cur 指向链表的头节点,随后开始对链表进行遍历。如果当前 cur 与 cur.next 对应的元素相同,那么我们就将 cur.next 从链表中移除;否则说明链表中已经不存在其它与 cur 对应的元素相同的节点,因此可以将cur 指向 cur.next。
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if (!head) {
//若是为空则直接返回
return head;
}
ListNode* cur = head;
while (cur->next) {
//由于已经判断了不为空,则判断下一个cur->next
//将当前节点与后面的节点挨个比较即可
if (cur->val == cur->next->val) {
cur->next = cur->next->next;
}
else {
cur = cur->next;
}
}
return head;
}
};