当前位置:网站首页>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
边栏推荐
猜你喜欢

Sim Api User Guide(5)

IDEA——》每次启动都会Indexing或 scanning files to index

Question bank and answers of Shanghai safety officer C certificate examination in 2022

【无标题】
MapReduce计算流程详解

2022年制冷与空调设备运行操作考试练习题及模拟考试

Yarn resource scheduler

Windows安装redis并将redis设置成服务开机自启

一文看懂 LSTM(Long Short-Term Memory)

Juc并发编程06——深入剖析队列同步器AQS源码
随机推荐
实践六 Windows操作系统安全攻防
lnmp的配置
Depth selector
Arm debugging (1): two methods to redirect printf to serial port in keil
net start mysql MySQL 服务正在启动 . MySQL 服务无法启动。 服务没有报告任何错误。
Jerry's more accurate determination of abnormal address [chapter]
[untitled]
Go language practice mode - functional options pattern
IDEA——》每次启动都会Indexing或 scanning files to index
第二章 In-Memory 体系结构 (IM-2.2)
101. Symmetric Tree
SQL tuning series - SQL performance methodology
[untitled]
Common DBA SQL statements (4) - Top SQL
Go language practice mode - functional options pattern
Common SQL statements of DBA (6) - daily management
NEC infrared remote control coding description
Detailed explanation of MapReduce calculation process
Turn: Maugham: reading is a portable refuge
通过流式数据集成实现数据价值(2)