1.29补1.27的卡
帮你把链表细节学清楚! | LeetCode:24. 两两交换链表中的节点_哔哩哔哩_bilibili
第一眼思路:有思考出交换顺序,可是没有想到如何将切断的点储存起来,以及循环判断条件
cur->next!=NULL是判断偶数节点的终止条件,cur->next->next!=NULL是判断奇数节点的。
cur->next!=NULL&&cur->next->next!=NULL要同时满足(奇数个节点就不用交换了),所以只有后面满足偶数个节点时才进入循环
本题通过引入一个虚拟头结点完成:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode *dummynode=new ListNode;
ListNode *temp,*temp1,*cur;
dummynode->next=head;
cur=dummynode;
while(cur->next!=NULL&&cur->next->next!=NULL)
{
temp=cur->next;
temp1=cur->next->next->next;
cur->next=cur->next->next;
cur->next->next=temp;
temp->next=temp1;
cur=cur->next->next;
}
return dummynode->next;
}
};
2、19. 删除链表的倒数第 N 个结点 - 力扣(LeetCode)
思路:统计链表的长度,然后找到我们要删除哪一个元素(跳过即可),虽然我知道这是很幼稚的方法,但是我还是写了
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode *dummynode=new ListNode(0);
dummynode->next=head;
ListNode *cur;
cur=dummynode;
int ct=0;
while(cur->next!=NULL)
{
cur=cur->next;
ct++;
}
cur=dummynode;
int p;
p=ct-n;
while(p--)
{
cur=cur->next;
}
cur->next=cur->next->next;
return dummynode->next;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode *dummynode=new ListNode(0);
dummynode->next=head;
ListNode *slow,*fast;
fast=dummynode;
slow=dummynode;
n++;
while(n--&&fast!=NULL)
{
fast=fast->next;
}
while(fast!=NULL)
{
fast=fast->next;
slow=slow->next;
}
slow->next=slow->next->next;
return dummynode->next;
}
};
看了卡哥的视频,简直就是绝妙!通过利用双指针,一个快指针,一个慢指针,快指针先走n步,再快慢指针同时走,等到快指针走到null时,慢指针走了count-n,就是
面试题 02.07. 链表相交 - 力扣(LeetCode)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
int lena=0,lenb=0;
ListNode *cura,*curb;
cura=headA;
curb=headB;
while(cura!=NULL)
{
cura=cura->next;
lena++;
}
cura=headA;
while(curb!=NULL)
{
curb=curb->next;
lenb++;
}
curb=headB;
if(lena<lenb)
{
swap(lena,lenb);
swap(cura,curb);
}
int gap=lena-lenb;
while(gap--)
{
cura=cura->next;
}
while(cura!=NULL)
{
if(cura==curb)
return cura;
cura=cura->next;
curb=curb->next;
}
return NULL;
}
};
把环形链表讲清楚! 如何判断环形链表?如何找到环形链表的入口? LeetCode:142.环形链表II_哔哩哔哩_bilibili无思路
以下是学习的快慢指针法
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode *fast,*slow;
fast=head;
slow=head;
while(fast!=NULL&&fast->next!=NULL)
{
fast=fast->next->next;
slow=slow->next;
if(fast==slow)
{
ListNode *index1=fast;
ListNode *index2=head;
while(index1!=index2)
{
index2=index2->next;
index1=index1->next;
}
return index2;
}
}
return NULL;
}
};
在博客中看到了另一位uu的解法