当前位置:网站首页>Likou Brush Question Record 8.1-----206. Reverse linked list
Likou Brush Question Record 8.1-----206. Reverse linked list
2022-08-09 02:02:00 【@ Bai Gui】
一、题目
二、代码
/** * 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) {
//题目分析:
//Unified linked list format
ListNode* dummyhead = new ListNode(0); //Set a fake header node In order to unify the single-node and multi-node situations
dummyhead->next = head;
//current and previous
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;
// Node for stepping Nodes and pointers are still different
while(temp_node!=nullptr)
{
first_node=temp_node;
temp_node=first_node->next; //before the last steptemp都记录
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; //Construct a display 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;
}
};
三、运行结果
边栏推荐
猜你喜欢
力扣刷题记录7.1-----707. 设计链表
2022 PMP Project Management Certification Exam Registration Guide (1)
【图像增强】基于Step和Polynomial 滤波实现图像增强附matlab代码
typescript91-添加任务基本实现
任务五 处理连续型数据
『Another Redis DeskTop Manager』用了这款Redis可视化工具,分析效率提升12倍
右键新建缺少word、excel选项问题处理
力扣刷题记录10.1-----19. 删除链表的倒数第 N 个结点
PMP有什么答题技巧?
【Fiddler】Fiddler实现mock测试(模拟接口数据)
随机推荐
Go - 9 - data type - function
LeetCode每日两题01:二分查找 (均1200道)
【Fiddler】Fiddler实现mock测试(模拟接口数据)
MT4/MQL4入门到精通EA课程第二课-常用的功能函数
LeetCode每日两题02:轮转数组 (均1200道)
typescripet92-添加任务功能优化
如何在推荐系统中玩转知识图谱
2020.10.13开发日志
智能视频监控设计摄像头部分
HNUMSC-C语言第一课
Phenomenon 1 during RF debugging
力扣刷题记录2.1-----27. 移除元素
在树莓派上使用cpolar(番外篇2)
qps tps rps 区别
2022杭电多校第五场1007(生成函数+启发式合并+ntt)
2022PMP项目管理认证考试报考指南(1)
yii2的安装之路
Go-8-Gin框架
HCIP-R&S By Wakin自用笔记(2)OSPF之OSPF回顾、虚连接
Educational Codeforces Round 132 (Rated for Div. 2)