当前位置:网站首页>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;
}
}
边栏推荐
- A Closer Look at How Fine-tuning Changes BERT
- synchronized 解决共享带来的问题
- Learn Arduino with examples
- Step by step guide to setting NFT as an ens profile Avatar
- "Friendship and righteousness" of the center for national economy and information technology: China's friendship wine - the "unparalleled loyalty and righteousness" of the solidarity group released th
- "Designer universe": "benefit dimension" APEC public welfare + 2022 the latest slogan and the new platform will be launched soon | Asia Pacific Financial Media
- How to use information mechanism to realize process mutual exclusion, process synchronization and precursor relationship
- Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
- Analysis of pointer and array written test questions
- ESP series pin description diagram summary
猜你喜欢

Ruffian Heng embedded bimonthly, issue 49

Nacos Development Manual

【MySQL】数据库的存储过程与存储函数通关教程(完整版)

NFT smart contract release, blind box, public offering technology practice -- jigsaw puzzle

Artcube information of "designer universe": Guangzhou implements the community designer system to achieve "great improvement" of urban quality | national economic and Information Center
![[research materials] 2022 China yuancosmos white paper - Download attached](/img/b4/422dff0510bbe67f3578202d6e80b7.jpg)
[research materials] 2022 China yuancosmos white paper - Download attached

It's hard to find a job when the industry is in recession

Online yaml to CSV tool
![[cloud native] teach you how to build ferry open source work order system](/img/fb/507f763791235bd00bc8201e5d7741.png)
[cloud native] teach you how to build ferry open source work order system

A Closer Look at How Fine-tuning Changes BERT
随机推荐
好用的TCP-UDP_debug工具下载和使用
Online yaml to CSV tool
[Yugong series] creation of 009 unity object of U3D full stack class in February 2022
A Closer Look at How Fine-tuning Changes BERT
Flash return file download
Learn Arduino with examples
It's hard to find a job when the industry is in recession
从 SQL 文件迁移数据到 TiDB
NFT smart contract release, blind box, public offering technology practice -- jigsaw puzzle
sys.argv
Hcip day 16
[t31zl intelligent video application processor data]
Fibonacci sequence
ESP系列引脚说明图汇总
Artcube information of "designer universe": Guangzhou implements the community designer system to achieve "great improvement" of urban quality | national economic and Information Center
flask返回文件下载
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
Pyqt5 development tips - obtain Manhattan distance between coordinates
IoT -- 解读物联网四层架构
Database basic commands