24. 两两交换链表中的节点
困难:怎么循环交换,交换完之后前后连接都不变
注意:使用虚拟头节点进行操作,而不是从第一个开始操作这样模拟后面的循环和第一组就会相同, 我的问题就是从1开始操作到后面发现连接有点问题
弄清楚cur到底是哪个?
细节:
1、循环终止条件实际上是两个
2、要交换后面两个节点 cur得是在两个节点之前
代码
/**
* 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* dummyhead = new ListNode(0);
ListNode* cur=dummyhead;
dummyhead->next=head;
while(cur->next!=nullptr&&cur->next->next!=nullptr) {
ListNode* tmp = cur->next;
cur->next=cur->next->next;
tmp->next=cur->next->next;
cur->next->next=tmp;
cur=tmp;
}
return dummyhead->next;
}
};
19.删除链表的倒数第N个节点
注意点:
1、快慢指针
2、要不要虚拟头节点看一下要不要处理头节点
/**
* 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* right=head;
ListNode* dummyhead=new ListNode(0);
dummyhead->next=head;
ListNode* left=dummyhead;
while(n--) {
right=right->next;
}
while(right!=nullptr) {
right=right->next;
left=left->next;
}
ListNode* tmp=left->next;
left->next=left->next->next;
delete tmp;
ListNode* result = dummyhead->next;
delete dummyhead;
return result;
}
};
面试题 02.07. 链表相交|
错误代码:
/**
* 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) {
ListNode* cur1=headA;
ListNode* cur2=headB;
int flag1=1,flag2=1;
while(cur1==cur2) {
cur1=cur1->next;
cur2=cur2->next;
if(flag1&&cur1==NULL){
cur1=headB;
flag1=0;
}
if(flag2&&cur2==NULL){
cur2=headA;
flag2=0;
}
}
return cur1;
}
};
1、循环条件应该是cur1!=cur2
2、学会使用三元运算符 condition ? expression_if_true : expression_if_false
3、想想循环怎么结束
4、cur1=cur1->next;
5 、需要确保当指针移动到链表末尾时进行正确的重置,并且不要过多依赖flag
变量来控制这种重置行为。
正确代码:
/**
* 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) {
ListNode* cur1=headA;
ListNode* cur2=headB;
int flag1=1,flag2=1;
while(cur1!=cur2) {
cur1=cur1?cur1->next:headB;
cur2=cur2?cur2->next:headA;
}
return cur1;
}
};
142.环形链表II
思路:快慢指针,一个每次走2步一个1步 但是这样找不到第一个节点
我觉得用哈希表set好点
哈希表解法:
/**
* 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) {
unordered_set<ListNode*> st;
ListNode* cur=head;
while(cur!=nullptr) {
if(st.count(cur)) return cur;
else{
st.insert(cur);
cur=cur->next;
}
}
return nullptr;
}
};
快慢指针法:
注意点:
1、若有环快慢指针一定环内相遇,快指针的相对速度为1
2、慢指针为什么一定在第一圈就相遇,因为转不到第二圈
3、怎么找到入口???
从头结点出发一个指针,从相遇节点 也出发一个指针,这两个指针每次只走一个节点, 那么当这两个指针相遇的时候就是 环形入口的节点。
4、问题:开局fast与show是相等的!!!
代码:
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode* slow=head;
ListNode* fast=head;
do {
if(fast==nullptr||fast->next==nullptr) return nullptr;
slow=slow->next;
fast=fast->next->next;
}while(slow!=fast);
ListNode* index1=head;
ListNode* index2=slow;
while(index1!=index2) {
index1=index1->next;
index2=index2->next;
}
return index1;
}
};