当前位置:网站首页>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;
}
}
};
三、运行结果
边栏推荐
- SEMRush如何寻找关键词用于投放广告
- 虹科技术|如何阻止供应链攻击?
- etcd实现大规模服务治理应用实战
- PMP有什么答题技巧?
- The security of the pension insurance?Reliable?
- [深入研究4G/5G/6G专题-55]: L3信令控制-4-软件功能与流程的切分-CU网元的信令
- MT4/MQL4 entry to proficient foreign exchange EA tutorial Lesson 1 Getting to know MetaEditor
- mysql连接超过八小时报错
- MT4/MQL4入门到精通EA课程第二课-常用的功能函数
- 【Unity】判断鼠标是否点击在UI上
猜你喜欢
随机推荐
2020.10.13 Development log
德语翻译-德语在线批量翻译软件
qps tps rps 区别
软件测试技术之如何编写测试用例(5)
MT4/MQL4入门到精通外汇EA教程第一课 认识MetaEditor
Go-9-数据类型-函数
js实现数组去重的方式(7种)
LeetCode每日两题01:二分查找 (均1200道)
typescript91-添加任务基本实现
How to install yii2
德语翻译器在线翻译中文
Wireshark packet capture tool
全文翻译:EDPB关于VVA(虚拟语音助理)中处理个人数据的指南02/2021
字节输入流(InputStream)与字节输出流(OutputStream)
Duplicate class com.google.common.util.concurrent.ListenableFuture found in modules
力扣刷题记录1.5-----367. 有效的完全平方数
低代码开发创新企业应用构建模式
『Another Redis DeskTop Manager』用了这款Redis可视化工具,分析效率提升12倍
2022/8/8 比赛思维+状压dp
项目经理VS产品经理,二者到底有何不同?