当前位置:网站首页>17. Reverse the linked list
17. Reverse the linked list
2022-07-26 02:21:00 【linsa_ pursuer】
Given the head node of a single linked list pHead( The header node has a value , For example, in the figure below , its val yes 1), The length is n, After reversing the linked list , Return the header of the new linked list .
Data range : 0\leq n\leq10000≤n≤1000
requirement : Spatial complexity O(1)O(1) , Time complexity O(n)O(n) .
For example, when entering a linked list {1,2,3} when ,
After reversal , The original linked list becomes {3,2,1}, So the corresponding output is {3,2,1}.
Example 1
Input :
{1,2,3}
Copy the return value :
{3,2,1}
Copy
Example 2
Input :
{}
Copy the return value :
{}
Copy instructions :
Empty linked list will output empty
The code is as follows :
import lombok.ToString;
@ToString
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}public class Main {
public static void main(String[] args) throws Exception {
ListNode head = new ListNode(1);
ListNode headOne = new ListNode(2);
ListNode headTwo = new ListNode(3);
head.next = headOne;
headOne.next = headTwo;
System.out.println(head);
System.out.println(reverseList(head));
}
public static ListNode reverseList(ListNode head) {
// pre The pointer : Used to point to the inverted node , Initialize to null
ListNode pre = null;
// Current node pointer
ListNode cur = head;
// Loop iteration
while (cur != null) {
// curNext node , Always point to the current node cur The next node of
ListNode curNext = cur.next;
// The key to reversal : The current node points to its previous node ( Note that this is not a two-way linked list , No precursor pointer )
cur.next = pre;
// to update pre
pre = cur;
// Update the current node pointer
cur = curNext;
}
// Why return to pre? because pre Is the head node after inversion
return pre;
}
}边栏推荐
- I.MX6UL核心模块使用连载-Iot-6ULX核心模块简要介绍 (一)
- Acwing 379. hide and seek problem solution (minimum path non repeating point coverage)
- 还在用==0 null equal 判断空值吗,对isEmpty 和 isBlank有多少了解呢
- Are you still using ==0 null equal to judge null values? How much do you know about isempty and isblank
- Product thinking drives the construction of R & D management tools
- [C language brush leetcode] 814. Binary tree pruning (m)
- Master-slave replication in MySQL
- I.MX6UL核心模块使用连载-RTC测试 (十二)
- National standard gb28181 protocol video platform easygbs message pop-up mode optimization
- Ti AM335X工控模块矩阵键盘电路的设计与驱动移植
猜你喜欢

Yum install MySQL FAQ
![[cloud native] 4.1 Devops foundation and Practice](/img/09/5423540d0a4a11bc7162c5ab343a4d.png)
[cloud native] 4.1 Devops foundation and Practice

Ggplot2 learning summary

Acwing 379. hide and seek problem solution (minimum path non repeating point coverage)

2. Login - verification code function and saving login status

【云原生】4.1 DevOps基础与实战

国标GB28181协议视频平台EasyGBS消息弹框模式优化

我来图书馆小程序一键签到和一键抢位置工具

TI AM335x工控模块网络跟文件系统NFS的实现

I.MX6UL核心模块使用连载-eMMC读写测试 (四)
随机推荐
Niuke net question brushing training (I)
我来图书馆小程序签到流程分析
[2019] [paper notes] tunable THz broadband absorption based on metamaterials——
Kaggle registration method to solve the problem of man-machine verification
[2020] [paper notes] growth of bi2te3/cofeb double-layer heterojunction by magnetron sputtering——
Product thinking drives the construction of R & D management tools
U++ print information to screen and log
栈题目:文件的最长绝对路径
本地仓库导致的报错
17_表单数据
Be careful about bitmap, the "memory Assassin"~
npm link的简单介绍及使用
numpy.sort
I came to the library applet check-in process analysis
Mandatory interview questions: 1. shallow copy and deep copy_ Deep copy
[cloud native] 4.1 Devops foundation and Practice
Slow query log in MySQL
How to choose cloud note tool? What can I do with cloud notes?
TCP三次握手四次挥手
Digital warehouse: on the construction practice of digital warehouse in banking industry