24.两两交换链表中的节点
题目链接:https://leetcode.cn/problems/swap-nodes-in-pairs/description/
我的代码:
/*** 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_head = new ListNode;dummy_head->next = head;ListNode* p = dummy_head;while (p->next != nullptr && p->next->next != nullptr) {ListNode* temp1 = p->next->next->next;ListNode* temp2 = p->next;p->next = p->next->next;p->next->next = temp2;p->next->next->next = temp1;p = p->next->next;}head = dummy_head->next;delete dummy_head;return head;}
};
还是定义虚拟头节点,同时注意循环条件,存储临时节点等细节问题。
19.删除链表的倒数第N个节点
题目链接:https://leetcode.cn/problems/remove-nth-node-from-end-of-list/description/
快慢指针法:
/*** 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_head = new ListNode;dummy_head->next = head;ListNode* fast = dummy_head;ListNode* slow = dummy_head;while (n--) {fast = fast->next;}while (fast->next) {fast = fast->next;slow = slow->next;}ListNode* p = slow->next;slow->next = p->next;delete p;head = dummy_head->next;delete dummy_head;return head;}
};
定义一个虚拟头节点,先让快指针走n步,之后两指针同时走直到fast的next节点为空,此时慢指针指向倒数第n个节点的前一个,即可执行删除。
面试题 02.07.链表相交
题目链接:https://leetcode.cn/problems/intersection-of-two-linked-lists-lcci/description/
我的代码:
/*** 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 lengthA = 0;int lengthB = 0;while (curA != nullptr) {lengthA++;curA = curA->next;}while (curB != nullptr) {lengthB++;curB = curB->next;}curA = headA;curB = headB;if (lengthA < lengthB) {swap(lengthA, lengthB);swap(curA, curB);}int gap = lengthA - lengthB;while (gap--) {curA = curA->next;}while (curA != nullptr) {if (curA == curB) {//注意是节点相等不是节点值相等,写成curA->val == curB->val会答案错误return curA;}curA = curA->next;curB = curB->next;}return NULL;//不存在相应节点时别忘了返回NULL}
};
求出两链表长度,将A链表置为较长链表,然后将curA移动到与curB平齐的位置,开始比较。
142.环形链表II
题目链接:https://leetcode.cn/problems/linked-list-cycle-ii/description/
我的代码:
/*** 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 != nullptr && fast->next != nullptr) {fast = fast->next->next;slow = slow->next;if (fast == slow) {ListNode* index1 = head;ListNode* index2 = fast;while (index1 != index2) {index1 = index1->next;index2 = index2->next;}return index1;}}return NULL;}
};
快慢指针法判断有无环,fast走两步,slow走一步,若有环两指针必相遇。
对环入口位置的数学推导: