- 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* tmp = nullptr, *tmp1= nullptr;//用于保存前驱结点next发生改变的后继结点地址,即[1,2,3,4]中1和3的地址
while (cur -> next != nullptr && cur -> next -> next != nullptr) {
tmp = cur -> next;
tmp1 = cur -> next -> next -> next;
cur -> next = cur -> next -> next;
cur -> next -> next = tmp;
tmp -> next = tmp1;
cur = cur -> next -> next;//更新新一轮的cur指针,交换两节点,cur为两节点中较前结点的前驱结点地址
}
// return head;注意head指向[]1,2,3,4]中的1,样例输出[2,4,3],思考
return dummyHead -> next;
//以下思路未保存变更结点前驱结点地址,丢失了一半结点
// if (head == nullptr || head -> next == nullptr) return head;
// ListNode* slow = head, *quick = head -> next, *tmp = nullptr;
// while (quick) {
// tmp = quick -> next;
// quick -> next = slow;
// slow -> next = tmp;
// if (tmp != nullptr && tmp -> next != nullptr) {
// slow = tmp;
// quick = tmp -> next;
// }
// else if (tmp != nullptr && tmp -> next == nullptr) {
// slow = tmp;
// quick = nullptr;
// }
// else {
// quick = nullptr;
// }
// }
// while (slow != nullptr && quick != nullptr) {
// tmp = quick -> next;
// slow -> next = tmp;
// quick -> next = slow;
// slow = tmp;
// if (tmp != nullptr) {
// quick = tmp -> next;
// }
// }
// return head;
}
};
- 19.删除链表的倒数第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* quick = dummyHead, *slow = dummyHead;
while (n--) {
quick = quick -> next;
}
while (quick -> next != nullptr) {
slow = slow -> next;
quick = quick -> next;
}
ListNode* tmp = slow -> next;
slow -> next = slow -> next -> next;
delete tmp;
tmp = nullptr;
return dummyHead -> next;
}
};
- 02.07.面试题.链表相交
- 要注意,交点不是数值相等,而是指针相等
- 注意最后res并没有用处,当两链表不相交时,会错误返回A链表而不是nullptr
/**
* 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) {
int countA = 0, countB = 0;
int sub = 0;
ListNode* curA = headA, *curB = headB;
ListNode* res = nullptr;
while (curA != nullptr) {
curA = curA -> next;
countA++;
}
while (curB != nullptr) {
curB = curB -> next;
countB++;
}
sub = countA - countB;
curA = headA;
curB = headB;
if (sub > 0) {
while (sub--) {
curA = curA -> next;
}
}
else {
while (sub++) {
curB = curB -> next;
}
}
// res = curA;
while (curA != nullptr) {
if (curA == curB) {
// res = curA;
break;
}
curA = curA -> next;
curB = curB -> next;
}
// return res;
return curA;
}
};