今日任务
- 24. 两两交换链表中的节点
- 19.删除链表的倒数第N个节点
- 面试题 02.07. 链表相交
- 142.环形链表II
- 总结
详细布置
24. 两两交换链表中的节点
用虚拟头结点,这样会方便很多。
题目链接/文章讲解/视频讲解: 代码随想录
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode* FeakNode = new ListNode(0);
FeakNode->next = head;//初始化虚拟头
ListNode* cur = FeakNode; //cur从虚拟头开始
//每次除了需要互换的两个节点,上下结点也会被波及。
while(cur->next != nullptr && cur->next->next != nullptr){
ListNode* tmp1;
ListNode* tmp2;
tmp1 = cur->next;
tmp2 = cur->next->next->next;
cur -> next = tmp1 ->next;
cur->next->next = tmp1;
tmp1->next = tmp2;
cur = cur->next->next;
}
// 最后返回头后边的内容。
return FeakNode ->next;
}
};
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* FeakNode = new ListNode(0);
FeakNode -> next = head;
ListNode* fastIndex = FeakNode;
ListNode* slowIndex = FeakNode;
// 将faster指针和slow指针的位置分开分开出来n
while(n-- && fastIndex->next != nullptr){
fastIndex = fastIndex->next;
}
fastIndex = fastIndex->next;
// 将faster指针和slow指针的位置同时向前滚动。
while(fastIndex != nullptr){
fastIndex = fastIndex->next;
slowIndex = slowIndex->next;
}
slowIndex->next = slowIndex->next->next;
return FeakNode -> next;
}
};
- 时间复杂度: O(n)
- 空间复杂度: O(1)
面试题 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* curA = headA;
ListNode* curB = headB;
// 让AB并齐
//得到AB的长度
int lenA = 0;
int lenB = 0;
while(curA!=NULL){
curA = curA->next;
lenA++;
}
while(curB!= NULL){
curB = curB->next;
lenB++;
}
curA = headA;
curB = headB;
//让A 最长不论一开始是A长还是B长
if(lenB>lenA){
swap(curA,curB);
swap(lenA,lenB);//交换值
}
// 得到AB之间长度差,让A先到长度差那,然后再依次对齐
int gap = lenA - lenB;
while(gap--){
curA = curA->next;
}
// 现在AB已经对齐长度了,一块遍历,要是相同就可以返回
while(curA!=NULL){
if(curA == curB){
return curA;
}
curA =curA->next;
curB = curB->next;
}
// 没有就返回null
return NULL;
}
};
142.环形链表II
算是链表比较有难度的题目,需要多花点时间理解 确定环和找环入口,建议先看视频。
题目链接/文章讲解/视频讲解:代码随想录
*/ class Solution { public: ListNode *detectCycle(ListNode *head) { // ListNode* FeakNode = new ListNode(0); // FeakNode->next = head; // 标记fasternode和slownode 都从开头出发 ListNode* fasterNode = head; ListNode* slowNode = head; //我有些不明白如果是希望fasternode别冲出链条,为什么不使用fasternode->next->next != NULL while(fasterNode != NULL && fasterNode->next != NULL){ // 一开始都在head肯定要先动才行 fasterNode = fasterNode->next->next; slowNode = slowNode->next; // 相遇代表成环,而且目前的点在环内 if(fasterNode == slowNode){ // 从环中一点出发,从head头出发 ListNode* Noticefasterindex = fasterNode;//also slow index ListNode* Noticecurindex = head; while(Noticefasterindex !=Noticecurindex){ // 一直到两个点对上这代表进入环了 // 这一块不太理解,为什么相同的点就是入口点 //但是数学推论是这样的 Noticefasterindex = Noticefasterindex->next; Noticecurindex = Noticecurindex->next; } // 把对应index输出 return Noticecurindex; } } return NULL; } };