Leetcode刷题——链表

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 *longList, *shortList;
        int interval=0;
        ListNode *p = new ListNode(0);
        ListNode *q = new ListNode(0);
        p->next = headA;
        q->next = headB;
        // 计算链表的长度
        int lenA = ListLenght(p);
        int lenB = ListLenght(q);
        if(lenA>lenB){
            longList = p;
            shortList = q;
            interval = lenA-lenB;
        }
        else{
            longList = q;
            shortList = p;
            interval = lenB-lenA;

        }
       //将长的那一方进行移动
        while(interval--){
            longList = longList->next;
        }
        //找到两者相交的地方
        while(longList->next){
            // cout<<longList->next->val;
            if(longList->next==shortList->next){
                return longList->next;
            }
            longList = longList->next;
            shortList = shortList->next;
        }
        return NULL;     
    }
    int ListLenght(ListNode *head){
        ListNode *p = head;
        int count = 0;
        while(p->next){
            count++;
            p = p->next;
        }
        return count;
    }
};

反转链表
在这里插入图片描述

/**
 * 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 *p = new ListNode(0); //创建头节点
       p->next = head;
       ListNode *q = p->next;  
       p->next=nullptr;
       while(q){  //利用头插法
           ListNode *temp = q->next;
           q->next = p->next;
           p->next = q;
           q=temp;
       }
       return p->next;
    }
};

3.归并两个有序的链表
在这里插入图片描述

/**
 * 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* mergeTwoLists(ListNode* list1, ListNode* list2) {
        ListNode *ans = new ListNode(0); //创建结果链表的头节点
        ListNode *res = ans; //尾部结点
        ListNode *p = list1, *q = list2;
        while(p && q){
            if(p->val < q->val){
                res->next = p;
                p = p->next;
            }
            else{
                res->next = q;
                q = q->next;
            }
            res = res->next;
        }
        ListNode *rest; //判断是否有盈余
        rest = (p==nullptr)?q:p;   
        while(rest){
            res->next = rest;
            rest = rest->next;
            res = res->next;
        }
        res->next = nullptr;  //最后一个结点的尾部置为null
        return ans->next;
    }
};

4.从有序链表中删除重复节点
在这里插入图片描述

/**
 * 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* deleteDuplicates(ListNode* head) {
        if(!head || !head->next){
            return head;
        }
        ListNode *head1 = new ListNode();
        head1->next = head;
        ListNode *p = head1->next;
        ListNode *q = p->next;
        while(q){
            if(q->val == p->val){
                q=q->next;
                p->next = q;
            }
           else{
               p = p->next;
               q = q->next;
           }
        }
        p->next = nullptr;
        return head1->next;
    }
};

5.删除链表的倒数第 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 *pre = new ListNode(0);
        pre->next = head;
        ListNode *p = pre;
        ListNode *q = p->next;
        int len = listLen(pre);
        int index = len-n;
        int k = 0;
        while(q && k!=index){
            k++;
            p->next = q;
            p = p->next;
            q = q->next; 
        }
        p->next = q->next;
        return pre->next;
    }
    //计算带有头节点的链表的长度
    int listLen(ListNode *head){
        ListNode *p = head->next;
        int k = 0;
        while(p){
            k++;
            p = p->next;
        }
        return k;
    }
};

6.两两交换链表中的节点
在这里插入图片描述

/**
 * 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 || !head->next){
            return head;
        }
        ListNode *L = new ListNode(0);
        L->next = head;
        ListNode *pre = L;
        ListNode *p = head;
        ListNode *q = p->next;
        while(p && q){
            ListNode *p1 = q->next;
            pre->next = q;
            pre = pre->next;
            pre->next = p;
            pre = pre->next;
            p = p1;
            if(p && p->next){
                q = p->next;
            }
            else{
                break;
            }
        }
        if(p){
            pre->next = p;
            pre = pre->next;
        }
        pre->next = nullptr;
        return L->next;

    }
};

7.链表求和
在这里插入图片描述

/**
 * 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* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode *p1 = new ListNode(0);
        p1->next = l1;
        ListNode *p2 = new ListNode(0);
        p2->next = l2;
        p1 = reverseList(p1);
        p2 = reverseList(p2);
        int cur = 0;
         ListNode *res = new ListNode(0);
        ListNode *p = p1->next;
        ListNode *q = p2->next;
        ListNode *pp;
        while(p && q){
            int temp = (p->val +q->val+cur);
            cur = temp/10;
            p->val = temp%10;
            pp = p->next;
            p->next = res->next;
            res->next = p;
            p = pp;
            q = q->next;
        }
        if(q){
            p=q;
        }
        while(p){
            int temp = p->val+cur;
            p->val = temp % 10;
            cur = temp /10;
            pp = p->next;
            p->next = res->next;
            res->next = p;
            p = pp;
        }
       
        if(cur>0){
            ListNode *last = new ListNode(cur);
            last->next = res->next;
            res->next = last;
        }
        return res->next;
    }
    //将带有头结点的链表逆序
    ListNode* reverseList(ListNode* head){
        ListNode *pre = head;
        ListNode *p = pre->next;
        pre->next = nullptr;
        while(p){
            ListNode *q = p->next;
            p->next = pre->next;
            pre->next = p;
            p = q;
        }
        return pre;
    }
};

8.回文链表
在这里插入图片描述

/**
 * 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:
    bool isPalindrome(ListNode* head) {
        if(!head || !head->next) return true;
        stack<int> s;
        ListNode *p = head;
        //计算链表的长度
        int len = 0;
        while(p){
            len++;
            p = p->next;
        }
        int mid = len/2;
        int count = 0;
        p = head;
        while(p){
            count++;
            if(count<=mid){
                s.push(p->val);
                if(count==mid && len%2==1){
                   p = p->next;
                   count++;
                }
            }
            else{
                if(s.top()!=p->val) return false;
                else{
                    s.pop();
                }
            }
            p = p->next; 
        }
        if(s.empty()) return true;
        else 
            return false;
        
    }
};

9.分隔链表
在这里插入图片描述

/**
 * 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:
    vector<ListNode*> splitListToParts(ListNode* head, int k) {
        //计算链表长度
        ListNode *p = head;
        int len = 0;
        while(p){
            len++;
            p = p->next;
        }
        int ave = len/k;
        int rest = len%k;
        vector<ListNode *> res(k);
        p = head;
        for(int i=0;i<k;i++){
            res[i] = p;
            ListNode *tail = res[i];
            ListNode *q;
            for(int j=1;j<=ave;j++){
                q = p->next;
                tail->next = p;
                tail = tail->next;
                p = q;
                
            }
            if(rest){
                q = p->next;
                tail->next = p;
                tail = tail->next;
                rest--;
                p = q;
                
            }
            if(tail)
                tail->next = nullptr;
        }
        return res;
    }
};

10.链表元素按奇偶聚集
在这里插入图片描述

/**
 * 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* oddEvenList(ListNode* head) {
        if(!head || !head->next) return head;
        ListNode *Dtail = head;
        ListNode *Otail = head->next;
        ListNode *p = Dtail;
        ListNode *q = head->next;
        ListNode *t = q->next; 
        while(t && t->next){
           ListNode *temp = t->next->next;
           p->next = t;
           p=p->next;
           q->next = t->next;
           q = q->next;
           t = temp;
        }
        if(t){
            p->next = t;
            p = p->next;
        }
        p->next = Otail;
        q->next = nullptr;
        return Dtail;
        
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值