链表02

24. 两两交换链表中的节点

class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        ListNode * dummy_head = new ListNode(0);
        dummy_head->next = head;
        if(head == nullptr || head -> next == nullptr) return head;
      
   
        ListNode* cur = dummy_head;
        while(cur->next != nullptr && cur->next->next != nullptr) {
            ListNode* tmp = cur->next; // 记录临时节点
            ListNode* tmp1 = cur->next->next->next; // 记录临时节点

            cur->next = cur->next->next;    // 步骤一
            cur->next->next = tmp;          // 步骤二
            cur->next->next->next = tmp1;   // 步骤三

            cur = cur->next->next; // cur移动两位,准备下一轮交换
        }
        ListNode* result = dummy_head->next;
        delete dummy_head;
        return result;
    }
};

19. 删除链表的倒数第 N 个结点

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        if (head->next == nullptr && n == 1) {
            return nullptr;
        }
        ListNode *dummyHead = new ListNode(0);
        dummyHead -> next = head;
        ListNode *slowPtr = head;
        ListNode *fastPtr = head;
        ListNode *pre = dummyHead;
        while(fastPtr  != nullptr && n--)
        {
            fastPtr = fastPtr -> next;
        }
        while(fastPtr != nullptr)
        {
            fastPtr = fastPtr -> next;
            slowPtr = slowPtr -> next;
            pre = pre -> next;
        }
        
        pre -> next = pre -> next -> next;
        
        delete slowPtr;
        return dummyHead -> next;
        
    }
};

160. 相交链表

class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        int n1 = 0, n2 = 0;
        ListNode *ptrA = headA;
        ListNode *ptrB = headB;
        while(ptrA != NULL)
        {
            ptrA = ptrA -> next;
            n1++;
        }
        while(ptrB != NULL)
        {
            ptrB = ptrB -> next;
            n2++;
        }

        
        if(n1 >= n2)
        {
            ptrA = headA;
            int n = n1 - n2;
            ptrB = headB;
            while(ptrA != NULL && n--)
            {
                ptrA = ptrA -> next;
            }
            while(ptrA != NULL && ptrB != NULL)
            {
                if(ptrA == ptrB)
                    return ptrA;
                ptrA = ptrA -> next;
                ptrB = ptrB -> next;
            }
            return NULL;
        }
        else if(n1 < n2)
        {
            ptrA = headA;
            int n = n2 - n1;
           
            ptrB = headB;
            while(ptrB != NULL && n--)
            {
                ptrB = ptrB -> next;
            }
            while(ptrA != NULL && ptrB != NULL)
            {
                
                if(ptrA == ptrB)
                    return ptrA;
                ptrA = ptrA -> next;
                ptrB = ptrB -> next;
            }
            return NULL;
        }
        return NULL;
    }
};

142.环形链表II

/**
 * 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 *slow = head;
        ListNode *fast = head;
        
        while(fast != NULL&& fast-> next != NULL)
        {
            fast = fast -> next -> next;
            slow = slow -> next;
            if(fast == slow)
            {
                ListNode* index1 = fast;
                ListNode* index2 = head;
                while (index1 != index2) {
                    index1 = index1->next;
                    index2 = index2->next;
                }
                return index2;
            }
            
        }
        return NULL;
    }
};
posted @ 2025-07-14 21:13  skyler886  阅读(8)  评论(0)    收藏  举报