当前位置:网站首页>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;
}
}边栏推荐
- 1. Mx6ul core module uses serial NAND FLASH read / write test (III)
- 博云容器云、DevOps 平台斩获可信云“技术最佳实践奖”
- 主键B+ Tree,二级索引B+ Tree及对应的查询过程分析
- I.MX6UL核心模块使用连载-CAN、蜂鸣器测试 (十一)
- Are you still using ==0 null equal to judge null values? How much do you know about isempty and isblank
- 1205 Lock wait timeout exceeded; Try restarting transaction processing
- What are the functions of cloud notes, and how do browsers add cloud note plug-ins
- prometheus+process-exporter+grafana 监控进程的资源使用
- Binary logs in MySQL
- numpy.sort
猜你喜欢

I came to the library applet one click sign in and one click grab location tool

主键B+ Tree,二级索引B+ Tree及对应的查询过程分析

信息系统项目管理师---第十章沟通管理和干系人管理历年考题
![[C]详解语言文件操作](/img/12/4affa1d3fb3e4ee126e1c1e3872d9b.png)
[C]详解语言文件操作

obsidian移动端PC段同步

Quick start of adding, deleting, modifying and checking business

Bo Yun container cloud and Devops platform won the trusted cloud "technology best practice Award"

IDEA运行web项目出现乱码问题有效解决(附详细步骤)

prometheus+blackbox-exporter+grafana 监控服务器端口及url地址

Advantages of composition API
随机推荐
Prove that perfect numbers are even
17_ Form Data
Be careful about bitmap, the "memory Assassin"~
18_请求文件
AttributeError: ‘Document‘ object has no attribute ‘pageCount‘
数仓:银行业数仓的分层架构实践
JS add random pixel noise background to the page
Prometheus+blackbox exporter+grafana monitoring server port and URL address
1. Mx6ul core module uses serial EMMC read / write test (IV)
信息系统项目管理师---第十章沟通管理和干系人管理历年考题
I.MX6UL核心模块使用连载-TF卡读写测试 (五)
MySQL transaction isolation level
Slow query log in MySQL
Ti am335x industrial control module uses the Debian system of beaglebone (BBB)
C unit test
[2021] [paper notes] 6G technology vision - otfs modulation technology
LeetCode_ Dynamic programming_ Medium_ 264. Ugly number II
图解B+树的插入过程
Niuke net question brushing training (I)
TCP三次握手四次挥手