当前位置:网站首页>leetcode 206. Reverse linked list
leetcode 206. Reverse linked list
2022-06-13 01:03:00 【Didi dada】
- Reverse a linked list
Here's the head node of the list head , Please reverse the list , And return the inverted linked list .
Example 1:
Input :head = [1,2,3,4,5]
Output :[5,4,3,2,1]
Example 2:
Input :head = [1,2]
Output :[2,1]
Example 3:
Input :head = []
Output :[]
Tips :
The number range of nodes in the linked list is [0, 5000]
-5000 <= Node.val <= 5000
Advanced : The linked list can be inverted by iteration or recursion . Can you solve the problem in two ways ?
Method 1 : iteration ( Double pointer )
Ideas :
Set the current node cur, Previous node pre, The latter node next;
Traversing the linked list , change cur And pre Connection relationship of nodes , namely cur->next=pre. because pre The initial value of is the value pointed to by the last node of the linked list after inversion next, therefore pre Initialize to nullptr. And then update cur Next node .
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* cur = head;
ListNode* pre = nullptr;
while(cur){
ListNode* next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
}
return pre;
}
};
Complexity analysis :
- Time complexity :O(n);
- Spatial complexity :O(1);
Mode two : Stack
Traversing the linked list , Store the nodes on the stack separately , Then create a new head node , Take out the nodes in the stack separately , Connect behind the new head node .
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if (head==nullptr) return head;
ListNode* cur = head;
stack<ListNode*> node;
while(cur!=nullptr){
node.push(cur);
cur=cur->next;
}
ListNode* new_head=new ListNode(0);
ListNode* p = new_head;
while(node.size()){
ListNode* cur = node.top();
node.pop();
p->next = cur;
p = p->next;
}
p->next = nullptr;
return new_head->next;
}
};
Complexity analysis :
- Time complexity :O(n);
- Spatial complexity :O(n);
Mode three : recursive
class Solution {
public:
ListNode* help(ListNode* pre, ListNode* cur){
if(cur==nullptr) return pre;
ListNode* temp = cur->next;
cur->next = pre;
return help(cur, temp);
}
ListNode* reverseList(ListNode* head) {
return help(nullptr, head);
}
};
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head==nullptr || head->next == nullptr) return head;
ListNode* new_head = reverseList(head->next);
head->next->next = head;
head->next = nullptr;
return new_head;
}
};
Complexity analysis :
- Time complexity :O(n);
- Spatial complexity :O(n);
边栏推荐
- How the ET framework uses it to develop games
- What kind of experience is it to be a software test engineer in a state-owned enterprise: every day is like a war
- 深度学习训练多少轮?迭代多少次?
- Get preview of precast body
- Cards are unpredictable
- Traditional machine learning classification model predicts the rise and fall of stock prices
- Sequence table - find main element
- sort
- [JS component] custom paging
- Andersen global expands its business in northern Europe through cooperation agreements in Finland and Denmark
猜你喜欢

(01).NET MAUI实战 建项目
![[JS component] dazzle radio box and multi box](/img/2a/00620bee312972db93e1db4313385f.jpg)
[JS component] dazzle radio box and multi box

Et5.0 configuring Excel

三栏简约typecho主题Lanstar/蓝星typecho主题

pytorch是什么?解释pytorch的基本概念

Et5.0 value type generation

Deep learning model pruning

Several categories of software testing are clear at a glance
![[latex] insérer une image](/img/0b/3304aaa03d3fea3ebb93b0348c3131.png)
[latex] insérer une image

How to choose stocks? Which indicator strategy is reliable? Quantitative analysis and comparison of strategic benefits of ASI, VR, arbr, DPO and trix indicators
随机推荐
About_ int128
深度学习模型剪枝
What kind of experience is it to be a software test engineer in a state-owned enterprise: every day is like a war
[JS component] calendar
[North Asia server data recovery] data recovery case of Hyper-V service paralysis caused by virtual machine file loss
How to handle different types of data
单片机串口中断以及消息收发处理——对接受信息进行判断实现控制
Common skills of quantitative investment -- Drawing Part 1: Drawing stock closing price curve and ochl candle chart
pytorch和tensorflow有什么区别?
软件测试的几种分类,一看就明了
Pipeline pipeline project construction
Leetcode-12- integer to Roman numeral (medium)
ArrayList underlying source code
Liu Hui and introduction to nine chapter arithmetic and island arithmetic
Status of the thread
Plusieurs catégories de tests logiciels sont claires à première vue
Unitywebrequest asynchronous Download
What is dummy change?
Common skills for quantitative investment - indicators Chapter 3: detailed explanation of RSI indicators, their code implementation and drawing
Leetcode-13- Roman numeral to integer (simple)