当前位置:网站首页>力扣刷题记录8.1-----206. 反转链表
力扣刷题记录8.1-----206. 反转链表
2022-08-09 01:51:00 【@白圭】
一、题目


二、代码
/** * 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* reverseList(ListNode* head) {
//题目分析:
//统一链表格式
ListNode* dummyhead = new ListNode(0); //设置虚假的头结点 为了使得单节点和多节点情况统一
dummyhead->next = head;
//当前和上一个
ListNode* first_node = new ListNode(0);
first_node=head;
ListNode* second_node = new ListNode(0);
second_node=head;
ListNode* temp_node = new ListNode(0);
temp_node=nullptr;
if(head!=nullptr)
temp_node=head->next;
//第一个节点指向空
if(head!=nullptr)
head->next=nullptr;
// 步进用节点 节点与指针还是不同的
while(temp_node!=nullptr)
{
first_node=temp_node;
temp_node=first_node->next; //最后一步之前temp都记录
std::cout<<" second1 "<<second_node->val<<std::endl;
first_node->next=second_node;
second_node=first_node;
std::cout<<" first "<<first_node->val<<std::endl;
std::cout<<" second2 "<<second_node->val<<std::endl;
}
// ListNode *show_node;
// show_node=first_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;
return first_node;
}
};
三、运行结果

边栏推荐
猜你喜欢
随机推荐
OpenMLDB + Jupyter Notebook:快速搭建机器学习应用
The Best Open Source Web Application Firewall to Protect Your Web Applications
Grid布局介绍
Codeforces Round #809 (Div. 2)A~D1
Use of torchversion.transforms
企业里Foxmail邮箱问题解决方法汇总
[深入研究4G/5G/6G专题-55]: L3信令控制-4-软件功能与流程的切分-CU网元的信令
走向合规化的虚拟人直播
typescript89-展示任务列表功能
LeetCode每日一题:搜索插入位置 (均1200道)方法:二分查找
Go-7-RESTful API的设计
谷歌翻译下载-免费谷歌翻译软件下载
ROS2 ERROR: OpenGL 1.5 is not supported in GLRenderSystem::initialiseContext at C:\ci\ws\build...
[Cellular Automata] Simulation of emergency evacuation of disaster personnel under social force factors based on cellular automata with matlab code attached
字符串压缩
如何在群晖系统中安装cpolar(群晖6.X版)
网络安全基础-基本dos命令(一)
【信号去噪】基于Sage-Husa自适应卡尔曼滤波器实现海浪磁场噪声抑制及海浪磁场噪声的产生附matlab代码
gstreamer 记录
PostMan import certificate add certificate









