leetcode链表题目汇总

22 篇文章 0 订阅
本文涵盖了多种链表操作的算法实现,包括反转链表、链表区间反转、链表排序、查找回文和环形链表等。此外,还涉及合并两个有序链表、插入排序链表、在环形链表中找到环的入口节点等复杂操作。这些算法展示了链表数据结构在解决计算机科学问题中的应用。
摘要由CSDN通过智能技术生成

206. 反转链表

https://leetcode-cn.com/problems/reverse-linked-list/

/**
 * 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* prev=NULL;
        while(head!=NULL){
            ListNode* next=head->next;
            head->next=prev;
            prev=head;
            head=next;
        }
        return prev;
    }
};

92. 反转链表 II

https://leetcode-cn.com/problems/reverse-linked-list-ii/

/**
 * 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* reverseBetween(ListNode* head, int left, int right) {
        ListNode* in,out;
        ListNode* root=head;
        if(left==right)return head;
        int cnt=0;
        ListNode* prev=NULL;
        while(head!=NULL){
            cnt++;
            if(cnt==left){
                in=prev;
                prev=head;
                head=head->next;
                continue;
            }
            if(cnt>left&&cnt<=right){
                ListNode* next=head->next;
                head->next=prev;
                prev=head;
                head=next;
                if(cnt==right){
                    if(left==1){
                        root->next=head;
                        return prev;
                    }
                    ListNode* temp=in->next;
                    in->next=prev;
                    temp->next=head;
                    return root;
                }
                continue;
            }
            prev=head;
            head=head->next;
        }
        return root;
    }
};

143. 重排链表

https://leetcode-cn.com/problems/reorder-list/

/**
 * 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:
    void reorderList(ListNode* head) {
        if(head==NULL)return;
        stack<ListNode*>S;
        ListNode* root=head;
        int cnt=0;
        while(root!=NULL){
            cnt++;
            S.push(root);
            root=root->next;
        }
        ListNode* h=head;
        ListNode* now=head->next;
        if(now==NULL)return;
        for(int i=2;i<=cnt;i++){
            if(i%2==0){
                h->next=S.top();
                S.pop();
                h=h->next;
            }else{
                h->next=now;
                h=h->next;
                now=now->next;
            }
        }
        h->next=NULL;
        return;
    }
};

148. 排序链表

https://leetcode-cn.com/problems/sort-list/

/**
 * 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* a[110000];
    static int cmp(ListNode* x,ListNode* y){
        if(x->val<y->val)return 1;
        return 0;
    }
    ListNode* sortList(ListNode* head) {
        if(head==NULL)return head;
        int n=0;
        while(head!=NULL){
            n++;
            a[n]=head;
            head=head->next;
        }
        sort(a+1,a+n+1,cmp);
        ListNode* root=a[1];
        ListNode* now=root;
        for(int i=2;i<=n;i++){
            now->next=a[i];
            now=now->next;
        }
        now->next=NULL;
        return root;
    }
};

234. 回文链表

https://leetcode-cn.com/problems/palindrome-linked-list/

/**
 * 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* Q[110000];
    int cnt=0;
    bool isPalindrome(ListNode* head) {
        while(head){
            cnt++;
            Q[cnt]=head;
            head=head->next;
        }
        for(int i=1;i<=cnt/2;i++){
            if(Q[i]->val!=Q[cnt-i+1]->val)return false;
        }
        return true;
    }
};

21. 合并两个有序链表

https://leetcode-cn.com/problems/merge-two-sorted-lists/

/**
 * 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* l1, ListNode* l2) {
        if(l1==NULL)return l2;
        if(l2==NULL)return l1;
        ListNode* root=new ListNode();
        ListNode* now=root;
        for(;;){
            if(l1==NULL&&l2==NULL)break;
            if((l1!=NULL)&&(l2==NULL||(l1->val<l2->val))){
                now->next=l1;
                l1=l1->next;
            }else{
                now->next=l2;
                l2=l2->next;
            }
            now=now->next;
        }
        root=root->next;
        return root;
    }
};

147. 对链表进行插入排序

https://leetcode-cn.com/problems/insertion-sort-list/

/**
 * 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* insertionSortList(ListNode* head) {
        ListNode* root=new ListNode();
        root->val=head->val;
        head=head->next;
        while(head!=NULL){
            ListNode* prev=NULL;
            ListNode* now=root;
            while(now->val<head->val){
                prev=now;
                now=now->next;
                if(now==NULL)break;
            }
            if(prev==NULL){
                ListNode* temp=new ListNode();
                temp->val=head->val;
                temp->next=root;
                root=temp;
            }else{
                ListNode* temp=new ListNode();
                temp->next=now;
                temp->val=head->val;
                prev->next=temp;
            }
            head=head->next;
        }
        return root;
    }
};

142. 环形链表 II

https://leetcode-cn.com/problems/linked-list-cycle-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(1){
            if(fast==NULL)return NULL;
            fast=fast->next;
            if(fast==NULL)return NULL;
            fast=fast->next;
            slow=slow->next;
            if(fast==NULL)return NULL;
            if(fast==slow)break;
        }
        while(head!=slow){
            head=head->next;
            slow=slow->next;
        }
        return head;
    }
};

141. 环形链表

https://leetcode-cn.com/problems/linked-list-cycle/

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool hasCycle(ListNode *head) {
        for(int i=1;i<=20000;i++){
            if(head==NULL){
                return false;
            }
            head=head->next;
        }
        return true;
    }
};

23. 合并K个升序链表

https://leetcode-cn.com/problems/merge-k-sorted-lists/

/**
 * 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:
    struct data{
        int val,id;
        bool operator<(const data &up)const{
            return val>up.val;
        }
    };
    priority_queue<data>q;
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        ListNode* head=new ListNode();
        ListNode* tail=head;
        int n=lists.size();
        for(int i=0;i<n;i++){
            if(lists[i]!=NULL)q.push({lists[i]->val,i});
        }
        ListNode* temp;
        bool flag=0;
        while(q.size()){
            flag=1;
            data now=q.top();
            q.pop();
            tail->val=now.val;
            ListNode* po=new ListNode();
            tail->next=po;
            temp=tail;
            tail=po;
            lists[now.id]=lists[now.id]->next;
            if(lists[now.id]!=NULL){
                q.push({lists[now.id]->val,now.id});
            }
        }
        if(flag==0)return NULL;
        temp->next=NULL;
        return head;
    }
};

1669. 合并两个链表

参考:https://leetcode-cn.com/problems/merge-in-between-linked-lists/

/**
 * 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* mergeInBetween(ListNode* list1, int a, int b, ListNode* list2) {
        ListNode* L2H=list2;
        ListNode* L2T=L2H;
        while(L2T->next!=NULL)L2T=L2T->next;
        int id=0;
        ListNode* now=list1;
        ListNode* mark1;
        ListNode* mark2;
        for(;;){
            if(id==a-1)mark1=now;
            if(id==b+1)mark2=now;
            id++;
            now=now->next;
            if(now==NULL)break;
        }
        mark1->next=L2H;
        L2T->next=mark2;
        return list1;
    }
};

剑指 Offer 18. 删除链表的节点

参考:https://leetcode-cn.com/problems/shan-chu-lian-biao-de-jie-dian-lcof/

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* deleteNode(ListNode* head, int val) {
        ListNode* res=new ListNode();
        ListNode* tail=res;
        while(head){
            if(head->val!=val){
                ListNode* po=new ListNode();
                po->val=head->val;
                tail->next=po;
                tail=po;
            }
            head=head->next;
        }
        res=res->next;
        return res;
    }
};

6013. 合并零之间的节点

https://leetcode-cn.com/problems/merge-nodes-in-between-zeros/

/**
 * 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* mergeNodes(ListNode* head) {
        head=head->next;
        ListNode* now=head;
        while(now!=NULL){
            while(now->next->val>0){
                now->val+=now->next->val;
                now->next=now->next->next;
            }
            now->next=now->next->next;
            now=now->next;
        }
        return head;
    }
};

6018. 根据描述创建二叉树

参考:https://leetcode-cn.com/contest/weekly-contest-283/problems/create-binary-tree-from-descriptions/

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    map<int,TreeNode*>M;
    map<int,int>f;
    TreeNode* createBinaryTree(vector<vector<int>>& d) {
        for(int i=0;i<d.size();i++){
            int x=d[i][0],y=d[i][1],z=d[i][2];
            if(M.count(x)==0)M[x]=new TreeNode(),M[x]->val=x;
            if(M.count(y)==0)M[y]=new TreeNode(),M[y]->val=y;
            if(f.count(x)==0)f[x]=0;
            if(f.count(y)==0)f[y]=0;
            f[y]++;
            if(z==1)M[x]->left=M[y];
            if(z==0)M[x]->right=M[y];
        }
        int mark=0;
        for(auto x:f){
            if(x.second==0){
                mark=x.first;
                break;
            }
        }
        return M[mark];
    }
};
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* createBinaryTree(vector<vector<int>>& ds) {
        map<int,TreeNode*>L;
        set<int>C;
        for(auto x:ds){
            int fa = x[0];
            int ch = x[1];
            int re = x[2];
            if(L.find(fa)==L.end())L[fa]=new TreeNode(fa);
            if(L.find(ch)==L.end())L[ch]=new TreeNode(ch);
            if(re)L[fa]->left = L[ch];
            else L[fa]->right = L[ch];
            C.insert(ch);
        }
        int root = -1;
        for(auto pr:L)if(C.count(pr.first)==0)root=pr.first;
        return L[root];
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值