C++手撕代码之链表

本文展示了如何使用C++实现一系列链表操作,包括从字符串构建链表、链表的反转、区间反转、按K个一组翻转、合并有序链表、合并K个已排序链表、判断环、找公共节点、链表相加、排序和删除重复元素等。这些代码示例演示了链表数据结构的基本操作和算法实现。

目录

由字符串构建链表

反转链表

链表指定区间反转

每K个一组翻转链表

合并有序链表    

合并K个已排序链表

判断链表是否有环

两个链表的第一个公共节点

链表相加

单链表排序

链表奇偶重排

删除链表中重复元素

删除有序链表中重复元素II 本身的也不要了


由字符串构建链表

数字链表同理,把字符串换成数字就行了

#include <iostream>

struct ListNode {
    char val;
    ListNode* next;
    ListNode(char x) : val(x), next(nullptr) {}
};

ListNode* buildLinkedList(const std::string& str) {
    ListNode* head = nullptr;
    ListNode* curr = nullptr;

    for (char c : str) {
        ListNode* node = new ListNode(c);
        if (head == nullptr) {
            head = node;
            curr = node;
        } else {
            curr->next = node;
            curr = curr->next;
        }
    }
    return head;
}

void printList(ListNode* head) {
    ListNode* curr = head;
    while (curr != nullptr) {
        std::cout << curr->val << " ";
        curr = curr->next;
    }
    std::cout << std::endl;
}

int main() {
    std::string str = "abcde";

    ListNode* head = buildLinkedList(str);

    printList(head);

    return 0;
}

反转链表

输入"abcde"

#include <iostream>

struct ListNode {
    char val;
    ListNode* next;
    ListNode(char x) : val(x), next(nullptr) {}
};

ListNode* buildLinkedList(const std::string& str) {
    ListNode *head = nullptr;
    ListNode *cur = nullptr;
    for(auto c : str)
    {
        ListNode* node = new ListNode(c);
        if(head == nullptr){
            head = node;
            cur = node;
        }else{
            cur->next = node;
            cur = cur->next;
        }
    }
    return head;
}

void printList(ListNode* head) {
    ListNode* curr = head;
    while (curr != nullptr) {
        std::cout << curr->val << " ";
        curr = curr->next;
    }
    std::cout << std::endl;
}

ListNode* reverseList(ListNode* head)
{
    ListNode* prev = nullptr;
    ListNode* cur = head;
    while(cur != nullptr){
        ListNode* next = cur->next;
        cur->next = prev;
        prev = cur;
        cur = next;
    }
    return prev;
}

int main() {
    std::string str = "abcde";

    ListNode* head = buildLinkedList(str);

    head = reverseList(head);

    printList(head);

    return 0;
}

链表指定区间反转

#include <bits/stdc++.h>

struct ListNode
{
    int val;
    ListNode* next;
    ListNode(int x):val(x),next(nullptr){}
};

ListNode* buildList(std::string& num)
{
    ListNode* head = nullptr;
    ListNode* curr = nullptr;
    std::vector<std::string> nums;
    std::stringstream ss(num.substr(1,num.size()-2));
    std::string item;
    while(getline(ss,item,','))
    {
        nums.push_back(item);
    }
    for(auto a : nums)
    {
        ListNode* temp = new ListNode(std::stoi(a));
        if(head == nullptr){
            head = temp;
            curr = temp;
        }else{
            curr->next =temp;
            curr= curr->next;
        }
    }
    return head;
}

void printList(ListNode* head)
{
    ListNode* curr = head;
    while(curr != nullptr)
    {
        std::cout << curr-> val <<" ";
        curr = curr->next;
    }
    std::cout<<std::endl;
}

ListNode* reverseBetween(ListNode* head,int m,int n)
{
    if(head == nullptr || head->next == nullptr) return head;
    ListNode* newhead = new ListNode(0);
    newhead->next = head;
    ListNode* pre = newhead;
    ListNode* cur = head;
    for(int i = 1; i < m; i++)
    {
        pre = pre->next;
        cur = cur->next;
    }
    for(int i = m; i < n; i++)
    {
        ListNode* temp = cur->next;
        cur->next = temp->next;
        temp->next = pre->next;
        pre->next = temp;
    }
    return newhead->next;
}

int main()
{
    std::string num = "{1,2,3,4,5}";
    int m =2,n =4;
    ListNode* head = buildList(num);

    head = reverseBetween(head,m,n);

    printList(head);
    return 0;
}

时间复杂度On,空间复杂度O1 

每K个一组翻转链表

#include <bits/stdc++.h>

struct ListNode
{
    int val;
    ListNode* next;
    ListNode(int x):val(x),next(nullptr){}
};

ListNode* buildList(std::string& num)
{
    ListNode* head = nullptr;
    ListNode* curr = nullptr;
    std::vector<std::string> nums;
    std::stringstream ss(num.substr(1,num.size()-2));
    std::string item;
    while(getline(ss,item,','))
    {
        nums.push_back(item);
    }
    for(auto a : nums)
    {
        ListNode* temp = new ListNode(std::stoi(a));
        if(head == nullptr){
            head = temp;
            curr = temp;
        }else{
            curr->next =temp;
            curr= curr->next;
        }
    }
    return head;
}

void printList(ListNode* head)
{
    ListNode* curr = head;
    while(curr != nullptr)
    {
        std::cout << curr-> val <<" ";
        curr = curr->next;
    }
    std::cout<<std::endl;
}


ListNode* reverseKGroup(ListNode* head, int k) {
        // write code here
        if(head==nullptr || head->next == nullptr)
            return head;
        ListNode* tail = head;
        for(int i = 0; i < k; i++)
        {
       
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值