链表
前言
struct node{
int val;
struct node* next;
node(int v):val,next(nullptr){}
};
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
while(head && head->val == val){
ListNode* tmp = head;
head = head->next;
delete tmp;
}
ListNode* cur = head;
while(cur && cur->next){
if(cur->next->val == val){
ListNode* tmp = cur->next;
cur->next = tmp->next;
delete tmp;
}else{
cur = cur->next;
}
}
return head;
}
};
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode* tmpHead = new ListNode();
tmpHead->next = head;
ListNode* cur = tmpHead;
while(cur->next){
if(cur->next->val == val){
ListNode* tmp = cur->next;
cur->next = tmp->next;
delete tmp;
}else{
cur = cur->next;
}
}
return tmpHead->next;
}
};
- 重点是index从0开始,可以想象成数组,索引从0开始。
- 如果还是没明白index,就去看示例。
class MyLinkedList {
public:
struct node{
int val;
struct node* next;
node(int v):val(v),next(nullptr){}
};
MyLinkedList() {
m_size = 0;
m_head = new node(0);
}
int get(int index) {
if(index < 0 || index > m_size-1){
return -1;
}
node* cur = m_head->next;
while(index--){
cur = cur->next;
}
return cur->val;
}
void addAtHead(int val) {
node* newNode = new node(val);
node* tmp = m_head->next;
m_head->next = newNode;
newNode->next = tmp;
m_size++;
}
void addAtTail(int val) {
node* newNode = new node(val);
node* cur = m_head;
while(cur->next){
cur = cur->next;
}
cur->next = newNode;
newNode->next = nullptr;
m_size++;
}
void addAtIndex(int index, int val) {
if(index < 0){
addAtHead(val);
return ;
}
if(index > m_size){
return;
}
if(index == m_size){
addAtTail(val);
return ;
}
node* newnode = new node(val);
node* cur = m_head;
while(index--){
cur = cur->next;
}
node* tmpNext = cur->next;
cur->next = newnode;
newnode->next = tmpNext;
m_size++;
}
void deleteAtIndex(int index) {
if(index < 0 || index > m_size-1){
return ;
}
node* cur = m_head;
while(index--){
cur = cur->next;
}
node* tmp = cur->next;
cur->next = tmp->next;
delete tmp;
m_size--;
}
private:
int m_size;
struct node* m_head;
};
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* tmp = nullptr;
ListNode* cur = head;
ListNode* pre = nullptr;
while(cur){
tmp = cur->next;
cur->next = pre;
pre = cur;
cur = tmp;
}
return pre;
}
};
class Solution {
public:
ListNode* reverse(ListNode* cur,ListNode* pre){
if(cur == nullptr)return pre;
ListNode* temp = cur->next;
cur->next = pre;
return reverse(temp,cur);
}
ListNode* reverseList(ListNode* head) {
***
## [24. 两两交换链表中的节点](https:
return reverse(head,nullptr);
}
};
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode* tmpHead = new ListNode(0);
tmpHead->next = head;
ListNode* cur = tmpHead;
while(cur->next && cur->next->next){
ListNode* tmp = cur->next;
ListNode* tmp1 = cur->next->next->next;
cur->next = tmp->next;
cur->next->next = tmp;
tmp->next = tmp1;
cur = cur->next->next;
}
return tmpHead->next;
}
};
- 快慢指针
- 创建虚拟头结点,这样可以使链表中的所有元素可以统一进行管理。
- 如果先让快指针走n步,然后快慢指针同时走,直到fast == nullptr,此时,slow所指向的正是要删除的导数第n个结点。
- 要删除,所以我们需要让fast再多走一步,即走n+1步,再让他们同时移动,直到fast == nullptr,此时slow正指向是要删除结点的前一个结点。
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* tmpHead = new ListNode(0);
tmpHead->next = head;
ListNode* fast = tmpHead;
ListNode* slow = tmpHead;
n++;
while(n-- && fast)fast = fast->next;
while(fast){
slow = slow->next;
fast = fast->next;
}
ListNode* delNode = slow->next;
slow->next = delNode->next;
delete delNode;
return tmpHead->next;
}
};
- 相同题目-160. 相交链表
- 解题步骤:
- 分别求出两个链表的长度
- 求出长度的差值,将两个链表按结尾对齐
- 同时遍历两个链表,指针相同则返回,反之一直没找到,返回NULL。
- 开始没想懂为什么要尾部对齐,看不懂就看题目给的示例图。
- 可以看出,在某一个相同的节点之后,两个链表的剩余结点都是相等的。所以要尾对齐。
即公共尾部
。
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode* curA = headA;
ListNode* curB = headB;
int lenA = 0;
int lenB = 0;
while(curA){
curA = curA->next;
lenA++;
}
while(curB){
curB = curB->next;
lenB++;
}
curA = headA;
curB = headB;
if(lenB > lenA ){
swap(lenA,lenB);
swap(curA,curB);
}
int gep = lenA - lenB;
while(gep--){
curA = curA->next;
}
while(curA){
if(curA == curB)return curA;
curA = curA->next;
curB = curB->next;
}
return NULL;
}
};
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if(headA ==NULL || headB == NULL)return NULL;
ListNode* curA = headA;
ListNode* curB = headB;
while(curA != curB){
curA = curA == NULL ?headB:curA->next;
curB = curB == NULL ?headA:curB->next;
}
return curA;
}
};
- 主要流程
- 快慢指针同时出发,找到在环内的相遇点。
- 然后分别定义两个指针从head出发和从相遇点出发,二者最终相遇位置为环的入口。
- 快慢指针
- 慢指针一次走一下,快指针一次走来两步。
- 进入环内后,相对于慢指针来说,快指针在以每次走一格的速度接近他,所以早晚有一天会相遇。
- 在追上慢指针之前,快指针最少在环内已经走了一圈。
- 数学证明:
- 其实我觉得这里的数学证明看看就差不多了,没必要也没啥可总结的了,所以标记下可以学习的地方,笔记上一带而过。
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode* slow = head;
ListNode* fast = head;
while(fast && fast->next){
fast = fast->next->next;
slow = slow->next;
if(fast == slow){
ListNaode* index1 = fast;
ListNode* index2 = head;
while(index1 != index2){
index1 = index1->next;
index2 = index2->next;
}
return index1;
}
}
return NULL;
}
};
评论区