当前位置:网站首页>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);
边栏推荐
- spiral matrix visit Search a 2D Matrix
- Rasa对话机器人之HelpDesk (三)
- Leetcode-11- container with the most water (medium)
- 深度学习训练多少轮?迭代多少次?
- 三栏简约typecho主题Lanstar/蓝星typecho主题
- Common skills of quantitative investment -- Drawing Part 1: Drawing stock closing price curve and ochl candle chart
- Several categories of software testing are clear at a glance
- Matrix fast power
- spiral matrix visit Search a 2D Matrix
- MySQL exception: com mysql. jdbc. PacketTooBigException: Packet for query is too large(4223215 > 4194304)
猜你喜欢

深度学习模型剪枝

.net core 抛异常对性能影响的求证之路

Can GPU acceleration pytorch work?

5G工业网关在煤矿行业的应用优势

Three column simple Typecho theme lanstar/ Blue Star Typecho theme

Undirected graph -- computing the degree of a node in compressed storage

How to solve the duplication problem when MySQL inserts data in batches?

Cards are unpredictable

软件测试的几种分类,一看就明了

Binary tree - right view
随机推荐
Common skills of quantitative investment - index part 2: detailed explanation of BOL (Bollinger line) index, its code implementation and drawing
深度学习训练多少轮?迭代多少次?
AOF持久化
Cards are unpredictable
深度学习每周期的步数多少合适?
Androi天气
. The way to prove the effect of throwing exceptions on performance in. Net core
pytorch和tensorflow有什么区别?
What kind of experience is it to be a software test engineer in a state-owned enterprise: every day is like a war
How many steps are appropriate for each cycle of deep learning?
Plusieurs catégories de tests logiciels sont claires à première vue
Rest at home today
Quick power explanation
Common skills for quantitative investment - indicators Chapter 3: detailed explanation of RSI indicators, their code implementation and drawing
Common skills of quantitative investment -- Drawing Part 1: Drawing stock closing price curve and ochl candle chart
[JS component] custom paging
The seventh finals of the Blue Bridge Cup
Higherhrnet pre training model -- download from network disk
pytorch是什么?解释pytorch的基本概念
HashSet underlying source code