当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢

Problems in compiling core source cw32f030c8t6 with keil5

碳路先行,华为数字能源为广西绿色发展注入新动能

JVM (3): classloader classification and parental delegation mechanism

exness:流動性系列-訂單塊、不平衡(二)

CRMEB/V4.4标准版打通版商城源码小程序公众号H5+App商城源码

Guanghetong 5g module fg650-cn and fm650-cn series are produced in full scale, accelerating the efficient implementation of 5g UWB applications

JVM(3):类加载器分类、双亲委派机制

Unity 高级背包系统

From the first generation of sowing to the first generation of flowers, 5g commercial "gave birth to all things" for the third anniversary

CoDeSys get system time
随机推荐
What are the similarities and differences between the data center and the data warehouse?
决策树(Hunt、ID3、C4.5、CART)
Chia Tai International; What does a master account need to know
Unity music playback Manager
JVM (5): virtual machine stack, stack exception, stack storage result and operation principle, stack internal structure, local variable table
福州化工实验室建设注意隐患分析
Unity message framework notificationcenter
MySQL stored procedure
精益产品开发体系最佳实践及原则
The future has come and the 5g advanced era has begun
How can smart construction sites achieve digital transformation?
Legend has it that setting shader attributes with shader ID can improve efficiency:)
JVM (4): active and passive use of classes, internal structure of runtime data area, JVM thread description, PC register
lower_bound,upper_bound,二分
福州口罩洁净厂房建设知识概述
The second discussion class on mathematical basis of information and communication
lower_bound,upper_bound,二分
Record an ES accident
Record of serial baud rate
数据中台和数据仓库有什么异同?