24.两两交换链表中的结点|19.删除链表的倒数第n个结点|面试题.链表相交|142.环形链表②

  • 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;
    }
};
  • 142.环形链表2

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值