链表经典练习题及题解(c++)

前言:

        记录遇到的链表类题目,总结题解方法,加深对链表的理解,题目均来自在线平台。

 一.160. 相交链表 - 力扣(LeetCode)

思路1:

        分别遍历两个链表得出两个链表长度,然后长的链表向后移动长度之差步,接着长短链表同时移动,直到遇到相交结点或者无交点结束。

题解1:

/**
 * 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 *cur1 = headA,*cur2 = headB;
        int len1 = 0,len2 = 0;
        while(cur1)
        {
            cur1 = cur1->next;
            len1++;
        }
        while(cur2)
        {
            cur2 = cur2->next;
            len2++;
        }
        cur1 = headA;
        cur2 = headB;
        int k = len1 - len2;
        if(len1 >= len2)
        {
            while(k--)
                cur1 = cur1->next;
        }
        else
        {
            k *= -1;
            while(k--)
                cur2 = cur2->next;
        }
        while(cur1 != cur2)
        {
            cur1 = cur1->next;
            cur2 = cur2->next;
        }
        return cur1;
    }
};

思路2:

        一个链表遍历完之后又从另一个链表头开始出发。

题解2:

/**
 * 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 *cur1 = headA,*cur2 = headB;
        while(cur1 != cur2)
        {
            cur1 = cur1 == nullptr? headB : cur1->next;
            cur2 = cur2 == nullptr? headA : cur2->next;
        }
        return cur1;
    }
};

二.翻转类题目

1.206. 反转链表 - 力扣(LeetCode)

思路1:

        顺序遍历原链表,用原链表每个结点的值构造新结点,依次头插形成新链表。

题解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* reverseList(ListNode* head) {
        ListNode *dummy = new ListNode,*cur = head;
        while(cur)
        {
            ListNode *tmp = new ListNode(cur->val);
            tmp->next = dummy->next;
            dummy->next = tmp;
            cur = cur->next;
        }
        return dummy->next;
    }
};

思路2:

        多指针;

题解2:

/**
 * 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* reverseList(ListNode* head) {
        ListNode *cur = head,*pre = nullptr,*next = nullptr;
        while(cur)
        {
            next = cur->next;
            cur->next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }
};

 2.24. 两两交换链表中的节点 - 力扣(LeetCode)

思路1:

        递归。

题解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* swapPairs(ListNode* head) {
        if(head == nullptr || head->next == nullptr)
            return head;

        ListNode *tmp = swapPairs(head->next->next);
        ListNode *ret = head->next;
        ret->next = head;
        head->next = tmp;

你可能感兴趣的:(数据结构,链表,链表,c++,数据结构)