Leetcode0092 & 0206--Reverse Linked List 链表逆转
【转载请注明】http://www.cnblogs.com/igoslly/p/8670038.html
链表逆序在链表题目中还是较为常见的,这里将Leetcode中的两道题放在一起,分别是
0092 Reverse Linked List II 和 0206 Reverse Linked List,0092在0206的基础上,提出了部分逆序的概念
首先来看0206,题目描述为:Reverse a singly linked list.
也就是给出一个链表,要求我们将链表全部倒置链接
想法1: 定义一个堆栈,遍历链表时均push入堆栈,pop的顺序即为逆序
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
stack<ListNode*> list_stack; // 元素堆栈
ListNode *p = head; // 当链表为空时,直接返回空
if(head==NULL){return NULL;} // 循环结束后,p为链表最后一个有值的结点(非NULL)
while(p->next!=NULL){
list_stack.push(p);
p=p->next;
} // 逆序表表头为原链表末结点,即为p
head=p;
while(!list_stack.empty()){
ListNode *q = list_stack.top();
list_stack.pop();
p->next=q;
p=p->next;
} // 此时p是原链表首元素,新链表末元素,必须赋值NULL,否则提交回超时
p->next=NULL;
return head;
}
};
按照此方法提交后,Leetcode runtime 是 9ms
程序的时间复杂度是 O(2n),因为遍历两次;
空间复杂度是 O(n),stack大小取决于原链表的大小。
想法2: 在遍历原链表时,直接创造新逆序链表,想法大概如下:

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode * new_head=NULL;
while(head!=NULL){
ListNode * next=head->next;
head->next=new_head;
new_head=head;
head=next;
}
return new_head;
}
};
这种方法程序的时间复杂度,因为只遍历了一遍,是 O(n),同时也没有耗费多余的空间
再看题目0092,题目描述是
|
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: return Note: |
要求原链表里[m,n]位置的结点进行逆序 同时保证输入m,n合法 注意: 这里结点计算从1开始 |
相对与题1来说,题2中相对于逆序段的逆序过程不变,只是多出了逆序段前后结点连接的问题,涉及到的结点有:
①逆序段开始的前一个结点 before_modify ②逆序段开始的第一个结点 first_modify
③逆序段结束的最后一个结点 last_modify ④逆序段结束后的后一个结点 after_modify
正常的链表顺序是 ① -> ② -> ③ -> ④
逆序后链表顺序是 ① -> ③ -> ② -> ④
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
ListNode * pre_head =NULL,*p=head;
// pre_head=before_reverse, p=first_reverse
ListNode * new_head=NULL;
int cnt=; //count linked list position // 使用p和pre_head确定逆序段开始结点和前结点的位置
while(p!=NULL){
cnt++;
if(cnt==m){break;}
pre_head=p;p=p->next;
}
// 循环结束后,pre_head即为前结点位置(1),p为逆序段开始位置(2) // 因为后面循环p位置会改变,所以此时做下记录
// 在逆序后,此结点(2)就作为逆序段末结点,直接连接后续的结点
ListNode * reverse_first=p; // 逆序段开始逆序
while(cnt<=n){
ListNode *Next=p->next;
p->next=new_head;
new_head=p;
p=Next;
cnt++;
}
// 循环结束后,此时p为逆序段结束的后一个结点(4),连接在reverse_first(2)后面
reverse_first->next=p; // 注意:当m=1时,pre_head=NULL,直接返回逆序链表的new_head即可
// 否则,将new_head连接在前面正常后,返回原链表的head
if(pre_head!=NULL){
pre_head->next=new_head;return head;
}else{
return new_head;
}
}
};
Leetcode0092 & 0206--Reverse Linked List 链表逆转的更多相关文章
- 【LeetCode每天一题】Reverse Linked List(链表反转)
Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL ...
- [LeetCode]206. Reverse Linked List(链表反转)
Reverse a singly linked list. click to show more hints. Subscribe to see which companies asked this ...
- Leetcode 206 Reverse Linked List 链表
将单向链表反转 完成如图操作,依次进行即可 1 2 3 /** * Definition for singly-linked list. * struct ListNode { * int val; ...
- 【easy】206. Reverse Linked List 链表反转
链表反转,一发成功~ /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; ...
- [LeetCode] Reverse Linked List 倒置链表
Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...
- [LeetCode] Reverse Linked List II 倒置链表之二
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- LeetCode之“链表”:Reverse Linked List && Reverse Linked List II
1. Reverse Linked List 题目链接 题目要求: Reverse a singly linked list. Hint: A linked list can be reversed ...
- 反转链表 Reverse Linked List
2018-09-11 22:58:29 一.Reverse Linked List 问题描述: 问题求解: 解法一:Iteratively,不断执行插入操作. public ListNode reve ...
- 链表 206 Reverse Linked List, 92,86, 328, 2, 445
表不支持随机查找,通常是使用next指针进行操作. 206. 反转链表 /** * Definition for singly-linked list. * struct ListNode { * i ...
随机推荐
- bytes类型和python中编码的转换方法
一.bytes类型 bytes类型是指一堆字节的集合,在python中以b开头的字符串都是bytes类型.例如: >>> a = "中国" >>> ...
- Codeforces Round #413(Div. 1 + Div. 2, combined)——ABCD
题目在这里 A.Carrot Cakes 乱七八糟算出两个时间比较一下就行了 又臭又长仅供参考 #include <bits/stdc++.h> #define rep(i, j, k) ...
- Curious Cupid
There are K different languages in the world. Each person speaks one and only one language. There ar ...
- hdu 4975 最大流解决行列和求矩阵问题,用到矩阵dp优化
//刚开始乱搞. //网络流求解,如果最大流=所有元素的和则有解:利用残留网络判断是否唯一, //方法有两种,第一种是深搜看看是否存在正边权的环,见上一篇4888 //至少四个点构成的环,第二种是用矩 ...
- 刷新PHP缓冲区
为你的站点加速_php技巧 在当前 PHP 版本的默认配置下,“输出缓冲(Output Buffering)”是被打开的.旧版本则不是这样,在旧版本的 PHP 中,字符串在每次被输出的时候(通过 ec ...
- 正则表达式,字符串中需要两个反斜杠“\\d”
这个正则表达式为什么会有两个反斜杠? "^.*?\\.(jpg|png|bmp|gif)$"上面这个正则表达式为什么有两个反斜杠呢?反斜杠点\.就能表示点.了,为什么还要在\.前面 ...
- android 随手记之文件+參数上传请求
第一步:须要两个jar的支持,稍后以下给会出下载地址. 第二步:建立一个project 以下贴出最基本的代码 package com.example.testpaizhao; import java. ...
- CSS3选择器(全)
CSS选择器复习 通用选择器:* 选择到全部的元素 选择子元素:> 选择到元素的直接后代(第一级子元素) 相邻兄弟选择器:+ 选择到紧随目标元素后的第一个元素 普通兄弟选择器:~ 选择到紧随其后 ...
- hdu1285 拓扑排序+优先队列
原题地址 这算是我个人AC的第一个拓扑排序题目吧. 题目解读 给出几组比赛的胜负情况.推断最后的排名.依据题意这就是一个明显的拓扑排序问题了. 注意 假设由于可能的排名有多种情况,这时要保证编号小的在 ...
- runloop简单介绍
runloop是iOS底层机制中保持我们的程序一直运行的机制.他可以让线程一直循环不退出.而在我们正常的编程中.线程其实是线性的,当线程处理完我们的代码以后就自动退出了.runloop就是保证我们的应 ...