当前位置:网站首页>Leetcode question brushing series - mode 2 (datastructure linked list) - 206:reverse linked list
Leetcode question brushing series - mode 2 (datastructure linked list) - 206:reverse linked list
2022-06-11 04:37:00 【Dabie Mountains】
leetcode Brush questions series ---- Pattern 2(Datastructure Linked list )- 206:Reverse Linked List Reverse a linked list
Tips
- For more information, please refer to the catalogue of this series
- This problem can be solved by iteration .
- First define current and previous Two pointers .
- Use head preservation current Of next node .
- To break off current and current Of next The chain between .
- establish current and current Of next New link between ,current Of next Change to point previous.
- previous Be released , take current Turn into previous,head assignment current.
- Start a new iteration , Thought seems to have the shadow of a double pointer .
Python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
current = head
previous = None
while current:
temp = current.next
current.next = previous
previous = current
current = temp
return previous
C++
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* current = head;
ListNode* previous = nullptr;
// here head It's useless , Use head As caching
while(current!=nullptr)
{
head = current->next;
current->next = previous;
previous = current;
current = head;
}
return previous;
}
};
C#
/** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNode next; * public ListNode(int val=0, ListNode next=null) { * this.val = val; * this.next = next; * } * } */
public class Solution {
public ListNode ReverseList(ListNode head) {
ListNode current = head;
ListNode previous = null;
while(current!=null)
{
head = current.next;
current.next = previous;
previous = current;
current = head;
}
return previous;
}
}
Java
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */
class Solution {
public ListNode reverseList(ListNode head) {
ListNode current = head;
ListNode previous = null;
while(current!=null)
{
head = current.next;
current.next = previous;
previous = current;
current = head;
}
return previous;
}
}
边栏推荐
- 福州化工实验室建设注意隐患分析
- 把所有单词拆分成单个字词,删选适合公司得产品词库
- Unity 在不平坦的地形上创建河流
- PCB地线设计_单点接地_底线加粗
- Google Code Coverage best practices
- JVM (2): loading process of memory structure and classes
- Production of unity scalable map
- Problems in compiling core source cw32f030c8t6 with keil5
- Bas Bound, Upper Bound, deux points
- Data type conversion and conditional control statements
猜你喜欢

Guanghetong launched a new generation of 3GPP R16 industrial 5g module fg160 engineering sample

Vulkan official example interpretation raytracing

Detailed decomposition of the shortest path problem in Figure

The third small class discussion on the fundamentals of information and communication

New UI learning method subtraction professional version 34235 question bank learning method subtraction professional version applet source code

PCB ground wire design_ Single point grounding_ Bobbin line bold

JVM(1):介绍、结构、运行和生命周期

Unity 传送机制的实现

Redis master-slave replication, sentinel, cluster cluster principle + experiment (wait, it will be later, but it will be better)
![[server data recovery] data recovery case of RAID5 crash of buddy storage](/img/c4/681c542310f05192bd14757965ef0d.png)
[server data recovery] data recovery case of RAID5 crash of buddy storage
随机推荐
The future has come and the 5g advanced era has begun
碳路先行,华为数字能源为广西绿色发展注入新动能
Programming Examples Using RDMA Verbs
Redis主从复制、哨兵、cluster集群原理+实验(好好等,会晚些,但会更好)
MySQL stored procedure
JVM(4):类的主动使用与被动使用、运行时数据区域内部结构、JVM线程说明、PC寄存器
Chia Tai International: anyone who really invests in qihuo should know
Sharing of precautions for the construction of dioxin laboratory in Meizhou
Problems in compiling core source cw32f030c8t6 with keil5
JVM(1):介绍、结构、运行和生命周期
Analysis of hidden dangers in the construction of Fuzhou chemical laboratory
数字电影的KDM是什么?
lower_ bound,upper_ Bound, two points
Personalized use of QT log module
Record an ES accident
芯源cw32f030c8t6用keil5编译时出现的问题
Decision tree (hunt, ID3, C4.5, cart)
How the idea gradle project imports local jar packages
exness:流動性系列-訂單塊、不平衡(二)
Unity 可缩放地图的制作