当前位置:网站首页>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 )
边栏推荐
- Easy change
- Basic concept of bus
- 如何判断数组中是否含有某个元素
- What are the reasons for the frequent high CPU of ECS?
- Uninstall Google drive hard drive - you must exit the program to uninstall
- Principle and practice of common defects in RSA encryption application
- BUU-Crypto-Cipher
- Leakage detection relay jy82-2p
- 注释与注解
- Analysis of classical pointer and array written test questions in C language
猜你喜欢
Flask
724. Find the central subscript of the array
VB. Net GIF (making and disassembling - optimizing code, class library - 5)
VB.net GIF(制作、拆解——优化代码,类库——5)
LM small programmable controller software (based on CoDeSys) note XXI: error 3703
APScheduler如何设置任务不并发(即第一个任务执行完再执行下一个)?
Upper computer software development - log information is stored in the database based on log4net
复合非线性反馈控制(二)
Introduction To AMBA 简单理解
ANSYS command
随机推荐
VB. Net calls ffmpeg to simply process video (class Library-6)
Redis realizes ranking function
Arc135 C (the proof is not very clear)
left_and_right_net可解释性设计
BUU-Real-[PHP]XXE
px em rem的区别
LC周赛300
Grounding relay dd-1/60
【QT】制作MyComboBox点击事件
ANSYS command
JS how to convert seconds into hours, minutes and seconds display
Luogu deep foundation part 1 Introduction to language Chapter 5 array and data batch storage
Kubernets first meeting
Descriptive analysis of data distribution characteristics (data exploration)
安装 Pytorch geometric
JS string splicing enhancement
fastjson
JS flattened array of number shape structure
JS扁平化数形结构的数组
724. 寻找数组的中心下标