码迷,mamicode.com
首页 > 其他好文 > 详细

链表题(leetcode)

时间:2020-05-21 22:30:21      阅读:58      评论:0      收藏:0      [点我收藏+]

标签:反转   out   for   ast   std   types   leetcode   air   ons   

从头开始学习链表

链表遍历

#include <bits/stdc++.h>
#include <sys/types.h>
#include <unistd.h>
#define ll long long
#define mk make_pair
#define P pair<ll,ll>
const ll N = 1e6 + 10;
const double pi = acos(-1.0);
using namespace std;
struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};
int main()
{
    int n;
    cin >> n;
    ListNode *t = new ListNode(-1);
    ListNode *cur = t;
    for(int i=1;i<=n;i++)
    {
        int x;
        cin >> x;
        cur -> next = new ListNode(x);
        cur = cur -> next;
    }
    cur = t -> next;
    while(cur)
    {
        cout << cur -> val << ‘ ‘;
        cur = cur -> next;
    }
}

合并两个有序链表

#include <bits/stdc++.h>
#include <sys/types.h>
#include <unistd.h>
#define ll long long
#define mk make_pair
#define P pair<ll,ll>
const ll N = 1e6 + 10;
const double pi = acos(-1.0);
using namespace std;
struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};
ListNode* merge(ListNode *l1,ListNode *l2)
{
    ListNode *l3 = new ListNode(-1);
    ListNode * cur = l3;
    while(l1 && l2)
    {
        if(l1 -> val < l2 -> val)
        {
            cur -> next = l1;
            l1 = l1 -> next;
        }
        else
        {
            cur -> next = l2;
            l2 = l2 -> next;
        }
        cur = cur -> next;
    }
    if(l1)
        cur -> next = l1;
    else
        cur -> next = l2;
    return l3 -> next;
}
int main()
{
    int n,m;
    cin >> n >> m;
    ListNode *l1 = new ListNode(-1);
    ListNode *l2 = new ListNode(-1);
    ListNode *cur = l1;
    for(int i=1;i<=n;i++)
    {
        int x;
        cin >> x;
        cur -> next = new ListNode(x);
        cur = cur -> next;
    }
    cur = l2;
    for(int i=1;i<=m;i++)
    {
        int x;
        cin >> x;
        cur -> next = new ListNode(x);
        cur = cur -> next;
    }
    ListNode *l3 = merge(l1->next,l2->next);
    cur = l3;
    while(cur)
    {
        cout << cur->val << ‘ ‘;
        cur = cur -> next;
    }
    return 0;
}

链表相加

#include <bits/stdc++.h>
#include <sys/types.h>
#include <unistd.h>
#define ll long long
#define mk make_pair
#define P pair<ll,ll>
const ll N = 1e6 + 10;
const double pi = acos(-1.0);
using namespace std;
struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};
ListNode* add(ListNode *l1,ListNode *l2)
{
    ListNode *l3 = new ListNode(-1);
    ListNode * cur = l3;
    int t = 0;
    while(l1 || l2)
    {
        int x1 = 0,x2 = 0;
        if(l1)
            x1 = l1 -> val;
        if(l2)
            x2 = l2 -> val;
        int tt = x1 + x2 + t;
        cur -> next = new ListNode(tt%10);
        t = tt / 10;
        cur = cur -> next;
        if(l1)
            l1 = l1 -> next;
        if(l2)
            l2 = l2 -> next;
    }
    if(t)
        cur -> next = new ListNode(t);
    return l3 -> next;
}
int main()
{
    int n,m;
    cin >> n >> m;
    ListNode *l1 = new ListNode(-1);
    ListNode *l2 = new ListNode(-1);
    ListNode *cur = l1;
    for(int i=1;i<=n;i++)
    {
        int x;
        cin >> x;
        cur -> next = new ListNode(x);
        cur = cur -> next;
    }
    cur = l2;
    for(int i=1;i<=m;i++)
    {
        int x;
        cin >> x;
        cur -> next = new ListNode(x);
        cur = cur -> next;
    }
    ListNode *l3 = add(l1->next,l2->next);
    cur = l3;
    while(cur)
    {
        cout << cur->val << ‘ ‘;
        cur = cur -> next;
    }
    return 0;
}

排序链表

#include <bits/stdc++.h>
#include <sys/types.h>
#include <unistd.h>
#define ll long long
#define mk make_pair
#define P pair<ll,ll>
const ll N = 1e6 + 10;
const double pi = acos(-1.0);
using namespace std;
struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};
ListNode* merge(ListNode *l1,ListNode *l2)
{
    ListNode *l3 = new ListNode(-1);
    ListNode * cur = l3;
    while(l1 && l2)
    {
        if(l1 -> val < l2 -> val)
        {
            cur -> next = l1;
            l1 = l1 -> next;
        }
        else
        {
            cur -> next = l2;
            l2 = l2 -> next;
        }
        cur = cur -> next;
    }
    if(l1)
        cur -> next = l1;
    else
        cur -> next = l2;
    return l3 -> next;
}
ListNode* sort(ListNode *head)
{
    if(!head || !head->next)
        return head;
    ListNode *fast = head,*slow = head,*mid = head;
    while(fast)// 1 2 3
    {
        mid = slow;
        slow = slow -> next;
        if(fast->next)
            fast = fast -> next -> next;
        else
            break;

    }
    mid -> next = NULL;
    return merge(sort(head),sort(slow));
}
int main()
{
    int n;
    cin >> n;
    ListNode *l1 = new ListNode(-1);
    ListNode *cur = l1;
    for(int i=1;i<=n;i++)
    {
        int x;
        cin >> x;
        cur -> next = new ListNode(x);
        cur = cur -> next;
    }
    ListNode* l2 = sort(l1->next);
    cur = l2;
    while(cur)
    {
        cout << cur->val << ‘ ‘;
        cur = cur -> next;
    }
    return 0;
}

合并k个排序链表

#include <bits/stdc++.h>
#include <sys/types.h>
#include <unistd.h>
#define ll long long
#define mk make_pair
#define P pair<ll,ll>
const ll N = 1e6 + 10;
const double pi = acos(-1.0);
using namespace std;
struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};
ListNode* mergeTwoListNode(ListNode *l1,ListNode *l2)
{
    ListNode *l3 = new ListNode(-1);
    ListNode * cur = l3;
    while(l1 && l2)
    {
        if(l1 -> val < l2 -> val)
        {
            cur -> next = l1;
            l1 = l1 -> next;
        }
        else
        {
            cur -> next = l2;
            l2 = l2 -> next;
        }
        cur = cur -> next;
    }
    if(l1)
        cur -> next = l1;
    else
        cur -> next = l2;
    return l3 -> next;
}
ListNode *merge(vector<ListNode*>lists,int left,int right)
{
    if(left == right)
        return lists[left];
    int mid = (left + right) / 2;
    ListNode *l1 = merge(lists,left,mid);
    ListNode *l2 = merge(lists,mid+1,right);
    return mergeTwoListNode(l1,l2);
}
ListNode* mergeKLists(vector<ListNode*>& lists)
{
    int k = lists.size();
    ListNode *head = new ListNode(-1);
    return merge(lists,0,k-1);
}
int main()
{
    int n,k,x;
    cin >> k;
    vector<ListNode*> v;
    for(int i=1;i<=k;i++)
    {
        cin >> n;
        ListNode *head = new ListNode(-1);        ListNode *cur = head;
        for(int j=1;j<=n;j++)
        {
            cin >> x;
            cur->next = new ListNode(x);
            cur = cur -> next;
        }
        v.push_back(head->next);
    }
//    ListNode *cur = new ListNode(-1);
//    cur->next = v[0];
//    while(cur)
//    {
//        cout << cur -> val;
//        cur = cur -> next;
//    }
    ListNode *l1 = mergeKLists(v);
    while(l1)
    {
        cout << l1 -> val << ‘ ‘;
        l1 = l1 -> next;
    }
    return 0;
}
/*
 3
3 1 2 3
2 1 2
1 1
 1
 3 3 2 1
 */

删除链表倒数第k个节点

#include <bits/stdc++.h>
#include <sys/types.h>
#include <unistd.h>
#define ll long long
#define mk make_pair
#define P pair<ll,ll>
const ll N = 1e6 + 10;
const double pi = acos(-1.0);
using namespace std;
struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};
ListNode* eraseListNodeKth(ListNode *head,int k)
{
    ListNode *cur = head;
    int len = 0;
    while(cur)
    {
        cur = cur -> next;
        len++;
    }
    ListNode *ans = new ListNode(-1);
    ListNode *cur1 = head;
    cur = ans;
    for(int i=0;i<len;i++)
    {
        if(i==len-k)
        {
            cur1 = cur1 -> next;
        }
        if(!cur1)
            break;
        cur -> next = new ListNode(cur1->val);
        cur = cur -> next;
        cur1 = cur1 -> next;
    }
    return ans->next;
}
int main()
{
    int n,x,k;
    cin >> n >> k;
    ListNode* head = new ListNode(-1);
    ListNode* cur = head;
    for(int i=1;i<=n;i++)
    {
        cin >> x;
        cur -> next = new ListNode(x);
        cur = cur -> next;
    }
    ListNode* ans = eraseListNodeKth(head->next,k);
    while(ans)
    {
        cout << ans -> val << ‘ ‘;
        ans = ans -> next;
    }
    return 0;
}

链表反转

#include <bits/stdc++.h>
#include <sys/types.h>
#include <unistd.h>
#define ll long long
#define mk make_pair
#define P pair<ll,ll>
const ll N = 1e6 + 10;
const double pi = acos(-1.0);
using namespace std;
struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};
ListNode* reverseList(ListNode *head)
{
    ListNode *cur = NULL;
    ListNode *pre = head;
    while(pre)
    {
        ListNode *t = pre -> next;
        pre -> next = cur;
        cur = pre;
        pre = t;
    }
    return cur;
}
int main()
{
    int n,x,k;
    cin >> n;
    ListNode* head = new ListNode(-1);
    ListNode* cur = head;
    for(int i=1;i<=n;i++)
    {
        cin >> x;
        cur -> next = new ListNode(x);
        cur = cur -> next;
    }
    ListNode* ans = reverseList(head->next);
    while(ans)
    {
        cout << ans -> val << ‘ ‘;
        ans = ans -> next;
    }
    return 0;
}

链表去重

#include <bits/stdc++.h>
#include <sys/types.h>
#include <unistd.h>
#define ll long long
#define mk make_pair
#define P pair<ll,ll>
const ll N = 1e6 + 10;
const double pi = acos(-1.0);
using namespace std;
struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};
void deleteDuplicates(ListNode* head) {
    ListNode *cur = head;
    while(cur)
    {
        while(cur->next && cur->val == cur->next->val)
        {
            cur ->next = cur -> next -> next;
        }
        cur = cur -> next;
    }
}
int main()
{
    int n,x,k;
    cin >> n;
    ListNode* head = new ListNode(-1);
    ListNode* cur = head;
    for(int i=1;i<=n;i++)
    {
        cin >> x;
        cur -> next = new ListNode(x);
        cur = cur -> next;
    }
    deleteDuplicates(head->next);
    cur = head -> next;
    while(cur)
    {
        cout << cur -> val << ‘ ‘;
        cur = cur -> next;
    }
    return 0;
}

交换链表两个节点

#include <bits/stdc++.h>
#include <sys/types.h>
#include <unistd.h>
#define ll long long
#define mk make_pair
#define P pair<ll,ll>
const ll N = 1e6 + 10;
const double pi = acos(-1.0);
using namespace std;
struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};
ListNode* swap2(ListNode *head)
{
    if(!head || !head -> next)
        return head;
    ListNode *ans = new ListNode(-1);
    ans ->next = head;
    ListNode *cur = ans;
    while(cur -> next&& cur -> next->next) {
        ListNode *now = cur->next, *nxt = cur->next->next, *t = nxt->next;
        nxt->next = now;
        cur -> next = nxt;
        now->next = t;
        cur = now;
    }
    return ans -> next;
}
//-1 -> 1 -> 2 -> 3 -> 4 -> 5
// -1 -> 2 -> 1 -> 3 <- 4  5
int main()
{
    int n,x;
    cin >> n;
    ListNode *head = new ListNode(-1);
    ListNode *cur = head;
    for(int i=1;i<=n;i++)
    {
        cin >> x;
        cur -> next =  new ListNode(x);
        cur = cur -> next;
    }
    cur = swap2(head -> next);
    while(cur)
    {
        cout << cur -> val << ‘ ‘;
        cur = cur -> next;
    }
    return 0;
}

链表题(leetcode)

标签:反转   out   for   ast   std   types   leetcode   air   ons   

原文地址:https://www.cnblogs.com/hh13579/p/12933938.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!