当前位置:网站首页>Leetcode question brushing record | 206_ Reverse linked list
Leetcode question brushing record | 206_ Reverse linked list
2022-07-04 05:46:00 【coder_ sure】
leetcode Record of writing questions |206 _ Reverse a linked list
author github link : github link
Force to buckle 206 topic
type : Linked list
subject :
Here's the head node of the list head
, Please reverse the list , And return the inverted linked list .
Example 1
Input :head = [1,2,3,4,5]
Output :[5,4,3,2,1]
Example 2
Input :head = [1,2]
Output :[2,1]
Example 3
Input :head = []
Output :[]
Their thinking
Train of thought reminder : Three nodes are used to traverse
Train of thought details :
- Define two pointers :
prev
: Front pointer node ;curr:
Current pointer node - The front pointer node points to
null
,curr
On the linked listhead
It's about curr!=null
It's been circulating- Because the next step
prev
Yescurr->next,
The linked list behind the connection is about to be disconnected , This is our preparation in advance , To get anext
The pointer is used as a temporary mark for the following linked list , In order tocurr
Iterate back . curr->next
The value after the original link , Now let it disconnect , To make theprev
To connectcurr->next
.prev
Move back ,curr
Move back ( Here we use the previously prepared temporary variablesnext,
It's because ofnext
, To makecurr
Find the next position to continue the iteration )- Iteration complete , Returns the entire list , Implement inversion
c++
/** * 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) {
/* Define two pointers : prev: Front pointer node curr: Current pointer node */
ListNode * prev = NULL;// The front pointer node points to null
ListNode *curr = head;//curr On the linked list head It's about
while(curr){
//curr!=null It's been circulating
ListNode *next = curr->next;// Because the next step prev Yes curr->next, The linked list behind the connection is about to be disconnected , This is our preparation in advance , To get a next The pointer is used as a temporary mark for the following linked list , In order to curr Iterate back .
curr->next = prev;//curr->next The value after the original link , Now let it disconnect , To make the prev To connect curr->next
prev = curr;//prev Move back
curr = next;//curr Move back ( Here we use the previously prepared temporary variables next, It's because of next, To make curr Find the next position to continue the iteration )
}
return prev;// Iteration complete , Returns the entire list , Implement inversion
}
};
python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
prev = None
curr = head
while curr is not None:
next = curr.next
curr.next = prev
prev = curr
curr = next
return prev
Problem solving sketch paper ( For reference only , If there is any mistake, please correct it )
边栏推荐
- 每周小结(*63):关于正能量
- 配置交叉编译工具链和环境变量
- Supplement the JS of a video website to decrypt the video
- Flink1.13 basic SQL syntax (II) join operation
- JS扁平化数形结构的数组
- Notepad++ -- display related configurations
- HMS v1.0 appointment. PHP editid parameter SQL injection vulnerability (cve-2022-25491)
- JS arguments parameter usage and explanation
- Halcon image calibration enables subsequent image processing to become the same as the template image
- ANSYS command
猜你喜欢
Kubernets first meeting
体验碎周报第 102 期(2022.7.4)
[MySQL practice of massive data with high concurrency, high performance and high availability -8] - transaction isolation mechanism of InnoDB
How to expand all collapse panels
Principle and practice of common defects in RSA encryption application
Gridview出现滚动条,组件冲突,如何解决
Programmers don't talk about morality, and use multithreading for Heisi's girlfriend
My NVIDIA developer journey - optimizing graphics card performance
【微服务】Nacos集群搭建以及加载文件配置
BUU-Reverse-easyre
随机推荐
Kubernets first meeting
光模块字母含义及参数简称大全
安装 Pytorch geometric
谷歌 Chrome 浏览器将支持选取文字翻译功能
fastjson
Install pytoch geometric
(4) Canal multi instance use
Flink1.13 SQL basic syntax (I) DDL, DML
Introduction to AMBA
Solar insect killing system based on single chip microcomputer
C # character similarity comparison general class
How to determine whether an array contains an element
px em rem的区别
2022 a special equipment related management (elevator) examination questions simulation examination platform operation
Halcon image calibration enables subsequent image processing to become the same as the template image
云原生架构实战案例及优化解决方案
Input displays the currently selected picture
LM small programmable controller software (based on CoDeSys) note XXI: error 3703
input显示当前选择的图片
509. Fibonacci number, all paths of climbing stairs, minimum cost of climbing stairs