当前位置:网站首页>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,currOn the linked listheadIt's about curr!=nullIt's been circulating- Because the next step
prevYescurr->next,The linked list behind the connection is about to be disconnected , This is our preparation in advance , To get anextThe pointer is used as a temporary mark for the following linked list , In order tocurrIterate back . curr->nextThe value after the original link , Now let it disconnect , To make theprevTo connectcurr->next.prevMove back ,currMove back ( Here we use the previously prepared temporary variablesnext,It's because ofnext, To makecurrFind 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 )

边栏推荐
- JS扁平化数形结构的数组
- [microservice] Nacos cluster building and loading file configuration
- VB.net 简单的处理图片,黑白(类库——7)
- 1480. 一维数组的动态和
- 体验碎周报第 102 期(2022.7.4)
- Uninstall Google drive hard drive - you must exit the program to uninstall
- Use of hutool Pinyin tool
- Actual cases and optimization solutions of cloud native architecture
- ansys命令
- 接地继电器DD-1/60
猜你喜欢

Configure cross compilation tool chain and environment variables

Enterprise level log analysis system elk (if things backfire, there must be other arrangements)

我的NVIDIA开发者之旅——优化显卡性能

HMS v1.0 appointment.php editid参数 SQL注入漏洞(CVE-2022-25491)

Compound nonlinear feedback control (2)

VB.net 简单的处理图片,黑白(类库——7)

Online shrimp music will be closed in January next year. Netizens call No

Penetration tool - sqlmap

Gridview出现滚动条,组件冲突,如何解决

JS扁平化数形结构的数组
随机推荐
VB.net 调用FFmpeg简单处理视频(类库——6)
Input displays the currently selected picture
My NVIDIA developer journey - optimizing graphics card performance
Flink1.13 SQL basic syntax (I) DDL, DML
Ping port artifact psping
VB. Net simple processing pictures, black and white (class library - 7)
Actual cases and optimization solutions of cloud native architecture
"In simple language programming competition (basic)" part 1 Introduction to language Chapter 3 branch structure programming
SQL injection - injection based on MSSQL (SQL Server)
[Excel] 数据透视图
Flink1.13 basic SQL syntax (II) join operation
Gridview出现滚动条,组件冲突,如何解决
Analysis of classical pointer and array written test questions in C language
Overview of relevant subclasses of beanfactorypostprocessor and beanpostprocessor
BUU-Pwn-test_ your_ nc
Leakage detection relay jy82-2p
1480. Dynamic sum of one-dimensional array
安装 Pytorch geometric
BeanFactoryPostProcessor 与 BeanPostProcessor 相关子类概述
Flask