当前位置:网站首页>Linked list: reverse linked list
Linked list: reverse linked list
2022-06-13 02:45:00 【Zeng Qiang】
List of articles
subject
https://leetcode-cn.com/problems/UHnkqh/
Their thinking
Reverse the linked list , You can use double pointers pre and cur To solve .
It should be noted that :
- pre Pointer to null node ,cur Point to the head node .
- In exchange for pre and cur When you're pointing , Temporary storage required cur Of next node .
During backward traversal of the pointer , In exchange for cur.next = pre. So as to complete the inversion of the linked list . The specific steps are as follows :
Code
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */
class Solution {
public ListNode reverseList(ListNode head) {
ListNode pre = null;
ListNode cur = head;
while(cur != null) {
ListNode temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
return pre;
}
}
summary
Reverse a linked list , It mainly focuses on how to exchange the pointers of two linked lists , At the same time, cache the next node first , To continue traversing the linked list . This problem can be solved with double pointers .
边栏推荐
- [reading papers] comparison of deeplobv1-v3 series, brief review
- Android lightweight cache processing
- Resource arrangement
- redis
- Multiple knapsack problem
- Modify the color of El input, textarea and El checkbox when they are disabled
- Ijkplayer source code ---setdatasource
- Superficial understanding of conditional random fields
- Detailed explanation of UCI datasets and their data processing (with 148 datasets and processing codes attached)
- OpenCVSharpSample05Wpf
猜你喜欢

專業的數據庫管理軟件:Valentina Studio Pro for Mac

The latest Matlab r2020 B ultrasonic detailed installation tutorial (with complete installation files)

Vant框架中关于IndexBar索引栏的CDN单页面引用,无法正常展示

Laravel permission export

Detailed explanation of handwritten numeral recognition based on support vector machine (Matlab GUI code, providing handwriting pad)

Useful websites for writing papers and studying at ordinary times

js 解构赋值

04路由跳转并携带参数
![[data analysis and visualization] key points of data drawing 4- problems of pie chart](/img/e1/618ff53b33b4b1de6acf4942130c17.jpg)
[data analysis and visualization] key points of data drawing 4- problems of pie chart

Basic principle of bilateral filtering
随机推荐
Pycharm installation pyqt5 and its tools (QT designer, pyuic, pyrcc) detailed tutorial
Automatic differential reference
redis
Digital IC Design -- FIFO design
在IDEA使用C3P0连接池连接SQL数据库后却不能显示数据库内容
Impossible d'afficher le contenu de la base de données après que l'idée a utilisé le pool de connexion c3p0 pour se connecter à la base de données SQL
[reading papers] deep learning face representation by joint identification verification, deep learning applied to optimization problems, deepid2
[reading papers] deep learning face representation from predicting 10000 classes. deepID
redis 多个服务器共用一个
js 解构赋值
Rough understanding of wechat cloud development
Introduction to facial expression recognition system -- offline environment configuration
Opencvsharp4 pixel read / write and memory structure of color image and gray image
[reading papers] dcgan, the combination of generating countermeasure network and deep convolution
Find the number of permutations
04 route jump and carry parameters
Ijkplayer source code - audio playback
Welcome to blog navigation
遍历数组,删除某元素,直到删除为止
Vant框架中关于IndexBar索引栏的CDN单页面引用,无法正常展示