Leetcode 24. 两两交换链表中的节点
题目链接:24.两两交换链表中的节点
解题思路:使用虚拟头节点,正常模拟即可。自己做的时候有些边界会卡住,然后从而出bug。
/**
* 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* dummy = new ListNode(0);
dummy->next = head;
ListNode* cur = dummy;
while(cur->next != NULL && cur->next->next != NULL) {
ListNode* temp = cur->next;
ListNode* temp1 = cur->next->next;
cur->next = temp1;
temp->next = temp1->next;
temp1->next = temp;
cur = cur->next->next;
}
return dummy->next;
}
};
Leetcode 19.删除链表的倒数第N个节点
题目链接:19.删除链表的倒数第N个节点
解题思路:双指针的经典用法,如果要删除倒数第n个节点,让fast移动n步,然后让fast和slow同时移动,直到fast指向链表末尾。删除slow所指向的节点就可以了。但是我们结构体链表要删除slow所指向的节点,其实是要找到删除节点的上一个节点。因此fast要走n+1步
/**
* 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* dummy = new ListNode(0);
dummy->next = head;
ListNode* fast = dummy;
ListNode* slow = dummy;
while(n-- && fast != NULL) {
fast = fast->next;
}
fast = fast->next;
while(fast != NULL) {
slow = slow->next;
fast = fast->next;
}
ListNode* tmp = slow->next;
slow->next = slow->next->next;
delete tmp;
tmp = NULL;
return dummy->next;
}
};
Leetcode 160. 链表相交
题目链接:160.链表相交
解题思路:链表相交是指针相等,而不是数值相等;如果以数值相等的思路去做就会找错答案;求两个链表交点节点的指针。我们求出两个链表长度的差值,然后让curA移动到curB对齐的位置,并比较curA和curB是否相同即可。
/**
* 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, lenB = 0;
while(curA != NULL) {
lenA++;
curA = curA->next;
}
while(curB != NULL) {
lenB++;
curB = curB->next;
}
curA = headA;
curB = headB;
if(lenB > lenA) {
swap(lenB, lenA);
swap(curB, curA);
}
int gap = lenA - lenB;
while(gap--) {
curA = curA->next;
}
while(curA != NULL && curB != NULL) {
if(curA == curB) return curA;
curA = curA->next;
curB = curB->next;
}
return NULL;
}
};
Leetcode 142.环形链表II
题目链接:142.环形链表II
哈希法解题思路:这里环形链表,我们不仅要找到环,并且要找到环的起始点,那我用哈希法记录每个节点的出现次数即可,当有一个节点出现超过一次,他就是环的起点
双指针解题思路:先用快慢指针来判断链表中是否有环,快指针每次走2步,慢指针每次走1步,如果链表中有环,快慢指针总会遇到;那么遇到之后再如何入口处?当遇到的时候,只需在起点和当前位置定义两个指针(index1和index2),两个指针再一直走,当走到相遇的时候就是入口处,返回即可
哈希法:
/**
* 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_map<ListNode*, int> map;
if(head == NULL) return NULL;
ListNode* cur = head;
while(cur != NULL) {
map[cur]++;
if(map[cur] > 1) return cur;
cur = cur->next;
}
return 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* fast = head;
ListNode* slow = head;
while(fast != NULL && fast->next != NULL) {
fast = fast->next->next;
slow = slow->next;
if(slow == fast) {
ListNode* index1 = head;
ListNode* index2 = fast;
while(index1 != index2) {
index1 = index1->next;
index2 = index2->next;
}
return index2;
}
}
return NULL;
}
};
总结
文章链接:链表总结篇
经典题目:虚拟头结点、链表的基本操作、反转链表、删除倒数第N个节点、链表相交、环形链表