当前位置:网站首页>9.1-----24. Swap the nodes in the linked list in pairs
9.1-----24. Swap the nodes in the linked list in pairs
2022-08-09 02:06:00 【@baigui】
一、题目

二、代码
/** * 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)
{
// //统一链表格式
// ListNode* dummyhead = new ListNode(0); //设置虚假的头结点 为了使得单节点和多节点情况统一
// dummyhead->next = head;
ListNode* test_node = new ListNode(0); //探测节点 Check to see if there are more than two nodes after that
test_node=head;
if(test_node==nullptr) return test_node; //空节点直接返回
else if(test_node->next==nullptr) return test_node; //A single node is returned directly
else //大于两个节点 开始操作 比如 2 1 null
{
ListNode* return_node = new ListNode(0);
return_node=head->next;
ListNode* temp_node = new ListNode(0); //两大类情况 If there is no node in the linked list or there is only one node Then it should return directly ***最核心部分 Sentinel detection
ListNode* quick_node = new ListNode(0);
ListNode* slow_node = new ListNode(0);
temp_node=head->next->next;
quick_node=head->next;
slow_node=head;
int one_opeation=0;
while(test_node!=nullptr&&test_node->next!=nullptr)
{
std::cout<<" 进入 "<<std::endl;
// //Used to display the current node value
// std::cout<<"slow_node->val "<<slow_node->val <<std::endl;
// std::cout<<"quick_node->val "<<quick_node->val <<std::endl;
// std::cout<<"temp_node->val "<<temp_node->val <<std::endl;
test_node=test_node->next->next; // Each round of the probe pointer operates independently
if(one_opeation==1)
{
slow_node=temp_node;
quick_node=slow_node->next;
temp_node=quick_node->next;
}
if(one_opeation==0) one_opeation=1; //The first round is not required
quick_node->next=slow_node;
//temp_nodeThere are three situations when moving down nullprt 一个值 两个值
if(temp_node==nullptr) slow_node->next=temp_node;
else if (temp_node!=nullptr&&temp_node->next==nullptr) slow_node->next=temp_node;
else if(temp_node!=nullptr&&temp_node->next!=nullptr) slow_node->next=temp_node->next;
// //Used to display the current linked list
// ListNode *show_node;
// show_node=return_node; //构造处理节点
// while(show_node->next!=nullptr)
// {
// std::cout<<" "<<show_node->val<<" ";
// show_node=show_node->next;
// }
// std::cout<<" "<<show_node->val<<" "; //打印最后一个
// std::cout<<" "<< std::endl;
// //Used to display the current node value
// std::cout<<"slow_node->val "<<slow_node->val <<std::endl;
// std::cout<<"quick_node->val "<<quick_node->val <<std::endl;
// std::cout<<"temp_node->val "<<temp_node->val <<std::endl;
std::cout<<" 离开 "<<std::endl;
}
return return_node;
}
}
};
三、运行结果

边栏推荐
猜你喜欢

力扣刷题记录2.1-----27. 移除元素

考研人总结的时间管理7大忌,你中了几条?

《LC刷题总结》—— 二叉树

eladmin容器部署超详细过程

谷歌翻译下载-免费谷歌翻译软件下载

MT4 / MQ4L entry to the master of EA tutorial lesson two (2) - - MQL language commonly used function account information commonly used functions

JDBC技术(一)——一个简单的JDBC测试

德语翻译器在线翻译中文

日文翻译-在线免费日文翻译软件

数字孪生+燃气管理,开启智慧燃气管理新模式
随机推荐
MT4/MQL4入门到精通外汇EA教程第一课 认识MetaEditor
Go-11-流程控制
How SEMRush finds keywords for advertising
mysql 5.7 入坑
Latex example reference
webrtc 编译
2.1-----27. Remove elements
力扣刷题记录5.1-----59. 螺旋矩阵 II
2022/8/8 Competition thinking + state pressure dp
谷歌翻译软件-免费谷歌翻译
LeetCode每日两题01:有序数组的平方 (均1200道)方法:双指针
Design of Go-7-RESTful API
线段树知识整理
配置文件的读取-TOML
右键新建缺少word、excel选项问题处理
Dapr学习(4)之eShopOnDapr部署(Rancher2.63&k3s)
Codeforces Round #809 (Div. 2)A~D1
spdlog日志库的封装使用
etcd实现大规模服务治理应用实战
Using ngrok on Raspberry Pi (Extra 2)