前言:
更详细的在大佬的代码随想录 (programmercarl.com)
本系列仅是简洁版笔记,为了之后方便观看
删除元素
删除元素的时候要考虑要删除的结点是不是头节点,因为是与不是头节点的删除方式不一样
方法:
1.不用虚拟头结点,删除元素的时候要考虑要删除的结点是不是头节点,因为是与不是头节点的删除方式不一样
代码如下
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
//删除头节点
while (head != NULL && head->val == val) {//要先判断头结点是不是为空并且值是不是要删除的值
ListNode* tmp = head;//要创造一个一个临时变量,以便于后面进行删除
head = head->next;//直接进行头结点的转化
delete tmp;
}
ListNode* cur = head;
while (cur != NULL && cur->next!= NULL) {
if (cur->next->val == val) {
ListNode* tmp = cur->next;//要创造一个一个临时变量,以便于后面进行删除
cur->next = cur->next->next;//画图即可理解
delete tmp;
} else {
cur = cur->next;
}
}
return head;
}
};
2.用虚拟头结点
好处:代码比较统一,不需要考虑是不是头结点
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode* dummyHead = new ListNode(0); // 设置虚拟头结点,方法须记住
dummyHead->next = head;//将该节点指向原来的结点
ListNode* cur = dummyHead;
while (cur->next != NULL) {
if(cur->next->val == val) {
ListNode* tmp = cur->next;
cur->next = cur->next->next;
delete tmp;
} else {
cur = cur->next;
}
}
head = dummyHead->next;//记得还原原来的头结点
delete dummyHead;
return head;
}
};
设计链表
第一个节点从零开始
最关键的一点是操作的第n个点是cur->next;
插入结点的话的步骤要记清;
class MyLinkedList {
public:
// 定义链表节点结构体
struct LinkedNode {
int val;
LinkedNode* next;
LinkedNode(int val):val(val), next(nullptr){}
};
// 初始化链表
MyLinkedList() {
_dummyHead = new LinkedNode(0); // 定义虚拟头结点
_size = 0;//设定大小为0
}
// 获取到第index个节点数值,如果index是非法数值直接返回-1, 注意index是从0开始的,第0个节点就是头结点
int get(int index) {
if (index > (_size - 1) || index < 0) {
return -1;
}
LinkedNode* cur = _dummyHead->next;//这一行起到了很关键的作用
while(index--){ //
cur = cur->next;
}
return cur->val;
}
// 在链表最前面插入一个节点,插入完成后,新插入的节点为链表的新的头结点
void addAtHead(int val) {
LinkedNode* newNode = new LinkedNode(val);//定义新节点
newNode->next = _dummyHead->next;
_dummyHead->next = newNode;
_size++;//链表长度加1
}
// 在链表最后面添加一个节点
void addAtTail(int val) {
LinkedNode* newNode = new LinkedNode(val);
LinkedNode* cur = _dummyHead;
while(cur->next != nullptr){
cur = cur->next;
}
cur->next = newNode;
_size++;
}
void addAtIndex(int index, int val) {
if(index > _size) return;
if(index < 0) index = 0;
LinkedNode* newNode = new LinkedNode(val);
LinkedNode* cur = _dummyHead;
while(index--) {
cur = cur->next;
}
newNode->next = cur->next;
cur->next = newNode;
_size++;
}
// 删除第index个节点
void deleteAtIndex(int index) {
if (index >= _size || index < 0) {//边界条件的判断
return;
}
LinkedNode* cur = _dummyHead;
while(index--) {
cur = cur ->next;
}
LinkedNode* tmp = cur->next;
cur->next = cur->next->next;
delete tmp;
tmp=nullptr;//要设置成空指针
_size--;
}
// 打印链表
void printLinkedList() {
LinkedNode* cur = _dummyHead;
while (cur->next != nullptr) {
cout << cur->next->val << " ";
cur = cur->next;
}
cout << endl;
}
private:
int _size;
LinkedNode* _dummyHead;
};
翻转链表
题意:反转一个单链表。
示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL
双指针
//设置一个临时结点,用来保存会变化的cur->next的值,然后使用双指针进行翻转
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* temp; // 设置一个临时结点
ListNode* cur = head;
ListNode* pre = NULL;//这儿设置成空指针的原因是因为如果翻转过来的话,最后结点指向的是NULL
while(cur) {
temp = cur->next;
cur->next = pre; // 翻转操作
// 更新pre和cur指针
pre = cur;
cur = temp;
}
return pre;
}
};
递归
思路和双指针法是一样的,递归再调用函数的时候可以实现转换
class Solution {
public:
ListNode* reverse(ListNode* pre,ListNode* cur){
if(cur == NULL) return pre;
ListNode* temp = cur->next;
cur->next = pre;
return reverse(cur,temp);
}
ListNode* reverseList(ListNode* head) {
return reverse(NULL, head);
}
};
两两交换链表中的节点
1.链表的结点数如果是偶数的话,判断cur->next为空,循环就结束了,如果是奇数的话,需要判断cur->next->next为空;
2.特别注意的是while循环里面判断循环条件结束的顺序不能变,如果变了就会使得空指针异常
3.cur移动的位置是为了交换cur后的两个节点
lass Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode* dummyHead = new ListNode(0); // 设置一个虚拟头结点
dummyHead->next = head; // 将虚拟头结点指向head,这样方便后面做删除操作
ListNode* cur = dummyHead;
while(cur->next != nullptr && cur->next->next != nullptr) {
ListNode* tmp = cur->next; // 记录临时节点
ListNode* tmp1 = cur->next->next->next; // 记录临时节点
cur->next = cur->next->next; // 步骤一
cur->next->next = tmp; // 步骤二
cur->next->next->next = tmp1; // 步骤三
cur = cur->next->next; // cur移动两位,准备下一轮交换
}
ListNode* result = dummyHead->next;
delete dummyHead;
return result;
}
};
删除链表的倒数第N个节点
思路:双指针
删除倒数第n个节点,让fast移动n步,让fast和slow同时移动,直到fast指向链表末尾。删掉slow所指向的节点。
fast走n + 1步的原因是 因为只有这样同时移动的时候slow才能指向删除节点的上一个节点(方便做删除操作)
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* dummyHead = new ListNode(0);
dummyHead->next = head;
ListNode* slow = dummyHead;
ListNode* fast = dummyHead;
while(n-- && fast != NULL) {
fast = fast->next;
}
fast = fast->next; // fast再提前走一步,因为需要让slow指向删除节点的上一个节点
while (fast != NULL) {
fast = fast->next;
slow = slow->next;
}
slow->next = slow->next->next;
// ListNode *tmp = slow->next; C++释放内存的逻辑
// slow->next = tmp->next;
// delete tmp;
return dummyHead->next;
}
};
求两个链表交点节点的指针
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode* curA = headA;
ListNode* curB = headB;
int lenA = 0, lenB = 0;
while (curA != NULL) { // 求链表A的长度
lenA++;
curA = curA->next;
}
while (curB != NULL) { // 求链表B的长度
lenB++;
curB = curB->next;
}
curA = headA;
curB = headB;
// 让curA为最长链表的头,lenA为其长度
if (lenB > lenA) {
swap (lenA, lenB);
swap (curA, curB);
}
// 求长度差
int gap = lenA - lenB;
// 让curA和curB在同一起点上(末尾位置对齐)
while (gap--) {
curA = curA->next;
}
// 遍历curA 和 curB,遇到相同则直接返回
while (curA != NULL) {
if (curA == curB) {
return curA;
}
curA = curA->next;
curB = curB->next;
}
return NULL;
}
};
环形链表II
快指针至少在环里走上一圈才会和慢指针相遇,原因就是如果没有走上一圈,慢指针根本追不上快指针
fast指针走过的节点数 = slow指针走过的节点数 * 2:
(x + y) * 2 = x + y + n (y + z)
两边消掉一个(x+y):
x + y = n (y + z)
要求x ,将x单独放在左面:
x = n (y + z) - y
,再从n(y+z)中提出一个 (y+z)来,整理公式之后为如下公式:
x = (n - 1) (y + z) + z
注意这里n一定是大于等于1的,因为 fast指针至少要多走一圈才能相遇slow指针。先拿n为1的情况来举例,意味着fast指针在环形里转了一圈之后,就遇到了 slow指针了。
当 n为1的时候,公式就化解为
x = z
,这就意味着,从头结点出发一个指针,从相遇节点 也出发一个指针,这两个指针每次只走一个节点, 那么当这两个指针相遇的时候就是 环形入口的节点。
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode* fast = head;
ListNode* slow = head;
while(fast != NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
// 快慢指针相遇,此时从head 和 相遇点,同时查找直至相遇
if (slow == fast) {
ListNode* index1 = fast;
ListNode* index2 = head;
while (index1 != index2) {
index1 = index1->next;
index2 = index2->next;
}
return index2; // 返回环的入口
}
}
return NULL;
}
};
总结:
删除元素可以设置虚拟节点
ListNode* dummyHead = new ListNode(0);