当前位置:网站首页>[C topic] Li Kou 206. reverse the linked list
[C topic] Li Kou 206. reverse the linked list
2022-07-25 14:50:00 【GLC8866】
Method 1 : Create a new list , Its head node newhead At the beginning of NULL, Traverse the old linked list , If the node is not NULL, Insert its head into the new linked list . Insert once at each end ,newhead Just update it once ( By cur assignment ).
struct ListNode* reverseList(struct ListNode* head)
{
struct ListNode* cur=head;
struct ListNode* newhead=NULL;// Create a new head node , Traverse the old list , Insert it head ,NULL It will become the point of the tail node
while(cur)
{
struct ListNode* tmp=cur->next;
cur->next=newhead;
newhead=cur;// to update newhead
cur=tmp;// Check the next node of the old linked list
}
return newhead;
}Method 2 : Turn the pointer to the direction of the target .( I feel that the previous method is better )
struct ListNode* reverseList(struct ListNode* head)
{
struct ListNode* cur=head;// Mark the current node
struct ListNode* aim=NULL;// Will be cur Pointed node
if(cur==NULL)
return NULL;
while(cur->next!=NULL)// If there is a node behind the current node in the old linked list , take cur Move back one node , Re change cur The direction of .
{
struct ListNode* tmp=cur->next;
cur->next=aim;
aim=cur;
cur=tmp;
}
cur->next=aim;
return cur;
}
边栏推荐
- (original) customize a scrolling recyclerview
- English语法_不定代词 - other / another
- sudo rosdep init Error ROS安装问题解决方案
- 32 use of chrome debugging tools
- How many ways can you assign initial values to a two-dimensional array?
- 41 picture background synthesis - colorful navigation map
- LeetCode_字符串_中等_151.颠倒字符串中的单词
- 27 选择器的分类
- 二维数组赋初值你会几种方法?
- Syntax summary of easygui
猜你喜欢

阿里云安装MYSQL5.7

【MySQL必知必会】触发器 | 权限管理

The concept and operation rules of calculus of variations

006操作符简介

LeetCode-198-打家劫舍

Idea error failed to determine a suitable driver class

D2. Chopping Carrots (Hard Version) (每日一题)

sudo rosdep init Error ROS安装问题解决方案

Go language founder leaves Google

Why do China Construction and China Railway need this certificate? What is the reason?
随机推荐
PHP 通过原生CURL实现非阻塞(并发)请求模式
牛客多校 E G J L
51 single chip microcomputer learning notes (2)
44 新浪导航 ,小米边栏 练习
How many ways can you assign initial values to a two-dimensional array?
51 single chip microcomputer learning notes (1)
QObject source code analysis -d pointer and Q pointer
[eloquence] negotiation persuasion skills and Strategies
物理量与单位符号的书写标准
Paddlenlp之UIE关系抽取模型【高管关系抽取为例】
The input input box of H5 page pops up the numeric keypad, which needs to support decimal points
41 picture background synthesis - colorful navigation map
37 element mode (inline element, block element, inline block element)
Spark 参数配置的几种方法
GameFramework制作游戏(一)
变分(Calculus of variations)的概念及运算规则
I hope some suggestions on SQL optimization can help you who are tortured by SQL like me
Idea error failed to determine a suitable driver class
filters获取data中的数据;filters使用data中的数据
Go语言创始人从Google离职