当前位置:网站首页>2022.02.13 - NC001. Reverse linked list
2022.02.13 - NC001. Reverse linked list
2022-07-06 08:21:00 【A CAI continues to work hard】
List of articles
1. subject


2. Ideas
(1) Utilization stack ( Overtime )
- First use the stack push() Press the nodes into the stack in turn , Reuse stack pop() Pop up the nodes in turn , Reversal can be achieved , But it will time out .
(2) Discussion by situation
- Discuss separately 0 Nodes 、1 Nodes 、2 Nodes 、3 More than nodes :0 Nodes 、1 Nodes return directly head that will do ;2 Just reverse the nodes directly ;3 More than nodes need to use two variables to point to head Of next and next Of next,head Used to point to the node pointed to in the reverse operation ,next Used to point to the node pointed to in the reverse operation , That is, the head node of the new linked list ,nextNext Used to point to the head node of the original linked list .
(3) Optimize
- And (2) The idea is basically the same , But more concise .
- Variable last Point to the node pointed to in the reverse operation , Variable cur Point to the node pointed to in the reverse operation , That is, the head node of the new linked list , Variable next Point to the head node of the original linked list .
- Because the order in the loop body is moving variables next -> Reverse operation -> Moving variables last -> Moving variables cur, So finally, variables last Point to the head node of the new list .
3. Code
import java.util.Stack;
public class Test {
public static void main(String[] args) {
ListNode listNode1 = new ListNode(1);
ListNode listNode2 = new ListNode(2);
ListNode listNode3 = new ListNode(3);
listNode1.next = listNode2;
listNode2.next = listNode3;
Solution2 solution = new Solution2();
ListNode listNode = solution.ReverseList(listNode1);
System.out.println(listNode.val);
System.out.println(listNode.next.val);
System.out.println(listNode.next.next.val);
}
}
class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
class Solution {
public ListNode ReverseList(ListNode head) {
Stack<ListNode> stack = new Stack<>();
while (head != null) {
stack.push(head);
head = head.next;
}
head = stack.pop();
ListNode listNode = head;
while (!stack.isEmpty()) {
listNode.next = stack.pop();
listNode = listNode.next;
}
return head;
}
}
class Solution1 {
public ListNode ReverseList(ListNode head) {
if (head != null) {
// At least two points
if (head.next != null) {
// At least three points
if (head.next.next != null) {
ListNode next = head.next;
ListNode nextNext = next.next;
head.next = null;
while (nextNext != null) {
next.next = head;
head = next;
next = nextNext;
nextNext = next.next;
}
next.next = head;
head = next;
}
// There are only two points
else {
head.next.next = head;
head = head.next;
head.next.next = null;
}
}
}
return head;
}
}
class Solution2 {
public ListNode ReverseList(ListNode head) {
if (head == null) {
return null;
}
ListNode last = null;
ListNode cur = head;
ListNode next = null;
while (cur != null) {
next = cur.next;
cur.next = last;
last = cur;
cur = next;
}
return last;
}
}
边栏推荐
- On the day of resignation, jd.com deleted the database and ran away, and the programmer was sentenced
- logback1.3. X configuration details and Practice
- vulnhub hackme: 1
- VMware 虚拟化集群
- 2. File operation - write
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- Use Alibaba icon in uniapp
- 从表中名称映射关系修改视频名称
- synchronized 解决共享带来的问题
- LDAP應用篇(4)Jenkins接入
猜你喜欢

Wincc7.5 download and installation tutorial (win10 system)

你想知道的ArrayList知识都在这

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

Sanzi chess (C language)

Asia Pacific Financial Media | "APEC industry +" Western Silicon Valley invests 2trillion yuan in Chengdu Chongqing economic circle to catch up with Shanghai | stable strategy industry fund observatio
![[secretly kill little partner pytorch20 days -day01- example of structured data modeling process]](/img/ae/4e616882f6d68acdf8e885843e68a3.jpg)
[secretly kill little partner pytorch20 days -day01- example of structured data modeling process]

在 uniapp 中使用阿里图标

IOT -- interpreting the four tier architecture of the Internet of things

Yyds dry goods inventory three JS source code interpretation eventdispatcher

The resources of underground pipe holes are tight, and the air blowing micro cable is not fragrant?
随机推荐
08- [istio] istio gateway, virtual service and the relationship between them
Make learning pointer easier (3)
从 TiDB 集群迁移数据至另一 TiDB 集群
[research materials] 2022 enterprise wechat Ecosystem Research Report - Download attached
Upgrade tidb with tiup
leetcode刷题 (5.29) 哈希表
Let the bullets fly for a while
Online yaml to CSV tool
Asia Pacific Financial Media | designer universe | Guangdong responds to the opinions of the national development and Reform Commission. Primary school students incarnate as small community designers
hcip--mpls
Asia Pacific Financial Media | art cube of "designer universe": Guangzhou community designers achieve "great improvement" in urban quality | observation of stable strategy industry fund
Hcip day 16
C语言 - 位段
Artcube information of "designer universe": Guangzhou implements the community designer system to achieve "great improvement" of urban quality | national economic and Information Center
1204 character deletion operation (2)
String to leading 0
CAD ARX 获取当前的视口设置
Secure captcha (unsafe verification code) of DVWA range
从 CSV 文件迁移数据到 TiDB
2. File operation - write