当前位置:网站首页>206. Reverse linked list (linked list)
206. Reverse linked list (linked list)
2022-04-23 10:15:00 【Popuessing's Jersey】
The question : Invert a single chain table .
Example : Input : 1->2->3->4->5->NULL Output : 5->4->3->2->1->NULL
Method 1 : Double finger needling
public class Fanzhuanlianbiao {
static class ListNode{
int val;// data : Node data
ListNode next;// object , Reference next data object
// Construction method of linked list class
ListNode(int val){
this.val = val;
}
}
public void print (ListNode head){
while(head!=null){
System.out.print(head.val+" ");
head = head.next;
}
}
public ListNode reverseList(ListNode head){
// Define a temporary variable to save the next node of the current node
ListNode temp;
// Define the precursor node
ListNode pre = null;
// Define the current node
ListNode cur = head;
while(cur!=null){
// The temporary variable records the next node of the current node
temp=cur.next;
// The current node points to the predecessor node
cur.next = pre;
// The precursor node moves back one bit
pre = cur;
// Move the current node back one bit
cur = temp;
}
return pre;
}
public static void main(String[] args) {
ListNode node1 = new ListNode(1);
ListNode node2 = new ListNode(2);
ListNode node3 = new ListNode(3);
ListNode node4 = new ListNode(4);
ListNode node5 = new ListNode(5);
node1.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node5;
Fanzhuanlianbiao fanzhuanlianbiao = new Fanzhuanlianbiao();
ListNode node = node1;
fanzhuanlianbiao.print(node);
System.out.println();
ListNode res =fanzhuanlianbiao.reverseList(node1);
fanzhuanlianbiao.print(res);
}
}
Output results :
12345
54321
Method 2 : Recursive method
The idea is similar to double pointer , Pay attention to the end of recursion and recursive variables
// Recursive method
public ListNode reverseList(ListNode head){
return reverse(null,head);
}
private ListNode reverse(ListNode pre,ListNode cur){
// Recursive end point reached , return pre
if (cur==null){
return pre;
}
// Define temporary nodes temp Save the current node and the next node
ListNode temp = null;
temp = cur.next;
// reverse ( Current node refers to the forward node
cur.next = pre;
return reverse(cur,temp);
}
版权声明
本文为[Popuessing's Jersey]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231011141063.html
边栏推荐
- 一文读懂PlatoFarm新经济模型以及生态进展
- Pyqt5 and communication
- LeetCode 1249. Minimum Remove to Make Valid Parentheses - FB高频题1
- Examination questions and answers of the third batch (main person in charge) of Guangdong safety officer a certificate in 2022
- 0704、ansible----01
- 【无标题】
- Solve the problem of installing VMware after uninstalling
- Failureforwardurl and failureurl
- Juc并发编程06——深入剖析队列同步器AQS源码
- 通过流式数据集成实现数据价值(2)
猜你喜欢
随机推荐
打印页面的功能实现
Computer network security experiment II DNS protocol vulnerability utilization experiment
Sim Api User Guide(6)
【无标题】
Leetcode22:括号生成
Juc并发编程09——Condition实现源码分析
DBA常用SQL语句(2)— SGA和PGA
Formattime timestamp format conversion
《Redis设计与实现》
Juc并发编程06——深入剖析队列同步器AQS源码
Realizing data value through streaming data integration (5) - flow analysis
Sim Api User Guide(4)
net start mysql MySQL 服务正在启动 . MySQL 服务无法启动。 服务没有报告任何错误。
Sim Api User Guide(7)
基于PyQt5实现弹出任务进度条功能示例
Arm debugging (1): two methods to redirect printf to serial port in keil
Jerry sometimes finds that the memory has been tampered with, but there is no exception. How should he find it? [chapter]
DBA common SQL statements (5) - latch related
DBA常用SQL语句(6)- 日常管理
Sim Api User Guide(8)