24. 两两交换链表中的节点
给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。
/**
* 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,head);
ListNode* cur = dummyHead;
ListNode* temp1;
ListNode* temp2;
while(cur->next != NULL && cur->next->next != NULL)
{
temp1 = cur->next;
cur->next = cur->next->next;
temp2 = cur->next->next;
cur->next->next = temp1;
cur->next->next->next = temp2;
cur = cur->next->next;
}
ListNode* result = dummyHead->next;
delete dummyHead;
dummyHead = NULL;
return result;
}
};
- 临时节点的保存,当节点a由指向b转变为指向c,那么就会丢失对b的指向,如果后续还会用到b,就要用一个临时节点存放b;
- 空指针,如果用到了a->next,那么就需要判断a不是NULL,否则用到的a->next就是空指针,a->next->next等类推。
19.删除链表的倒数第N个节点
给你一个链表,删除链表的倒数第 n
个结点,并且返回链表的头结点。
/**
* 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* dummyhead = new ListNode(0,head);
ListNode* fastpoint = dummyhead;
ListNode* slowpoint = dummyhead;
n++;
while(n--&&fastpoint!=NULL)
{
fastpoint = fastpoint->next;
}
while(fastpoint!=NULL)
{
fastpoint = fastpoint->next;
slowpoint = slowpoint->next;
}
ListNode* temp = slowpoint->next;
slowpoint->next = slowpoint->next->next;
delete temp;
temp = NULL;
return dummyhead->next;
/*int num = 0;
ListNode* cur = head;
ListNode* dummyhead = new ListNode(0,head);
while(cur!=NULL)
{
num++;
cur = cur->next;
}
num = num-n;
cur = dummyhead;
while(num--&&cur->next!=NULL)
{
cur = cur->next;
}
ListNode* temp = cur->next;
if(cur->next!=NULL&&cur->next->next!=NULL)
cur->next = cur->next->next;
else
cur->next = NULL;
delete temp;
temp = NULL;
return dummyhead->next;*/
}
};
- 双指针法找倒数第n个节点。快指针先向前走n个节点,之后快慢指针同时向后走,当快指针走到NULL时,慢指针恰好走到倒数第n个,本题需要对倒数第n+1个节点进行操作,所以快指针先走n+1步;
- while(n--)循环n次,while(--n)循环n-1次。
02.07. 链表相交
给你两个单链表的头节点 headA
和 headB
,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null
。
/**
* 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;
int lenA = 0;
int lenB = 0;
while(curA != NULL)
{
lenA++;
curA = curA->next;
}
while(curB != NULL)
{
lenB++;
curB = curB->next;
}
curA = headA;
curB = headB;
if(lenB>lenA)
{
swap(lenA,lenB);
swap(curA,curB);
}
int len = lenA-lenB;
while(len--)
{
curA = curA->next;
}
while(curA != NULL)
{
if(curA == curB)
{
return curA;
}
curA = curA->next;
curB = curB->next;
}
return NULL;
/*//暴力解
ListNode* curA = headA;
ListNode* curB = headB;
while(curA!=NULL)
{
while(curB!=NULL)
{
if(curA == curB)
{
return curA;
}
curB = curB->next;
}
curB = headB;
curA = curA->next;
}
return NULL;*/
}
};
- 暴力解两个嵌套时间复杂度会高
- 先尾端对齐,之后让长链表指针走到和短链表第一个节点相同的位置,即两个指针后边的节点个数一致,之后两个链表同时向后遍历,找到相同的节点。
142.环形链表
给定一个链表的头节点 head
,返回链表开始入环的第一个节点。 如果链表无环,则返回 null
。
/**
* 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* fastpoint = head;
ListNode* slowpoint = head;
while(fastpoint !=NULL&&fastpoint->next !=NULL)
{
fastpoint = fastpoint->next->next;
slowpoint = slowpoint->next;
if(fastpoint == slowpoint)
{
ListNode* temp1 = fastpoint;
ListNode* temp2 = head;
while(temp1 != temp2)
{
temp1 = temp1->next;
temp2 = temp2->next;
}
return temp2;
}
}
return NULL;
}
};
- 快慢指针,快指针每次向后走两步,慢指针每次向后走1步,因为相对速度的存在,如果有环,两个指针必然会相遇;
- 经过推导,两个指针相遇的位置到环的入口和head到环的入口距离一致,推导见把环形链表讲清楚! 如何判断环形链表?如何找到环形链表的入口? LeetCode:142.环形链表II_哔哩哔哩_bilibili代码随想录