目录
由字符串构建链表
数字链表同理,把字符串换成数字就行了
#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++)
{

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

被折叠的 条评论
为什么被折叠?



