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 ...
随机推荐
- js用正则表达式将英文引号字符替换为中文引号字符
<script> $(function(){ var str='"我是英文版的引号",我要变成"中文版的引号"'; alert(replaceDqm ...
- 实验十二 团队作业8:软件测试与Alpha冲刺 第三天
项目 内容 这个作业属于哪个课程 老师链接 这个作业的要求在哪里 作业链接地址 团队名称 always run 作业学习目标 (1)掌握软件测试基础技术.(2)学习迭代式增量软件开发过程(Scrum) ...
- hdu2013 蟠桃记【C++】
蟠桃记 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- JavaSE 学习笔记之StringBuffer(十五)
--< java.lang >-- StringBuffer字符串缓冲区: 构造一个其中不带字符的字符串缓冲区,初始容量为 16 个字符. 特点: 1:可以对字符串内容进行修改. 2:是一 ...
- 清北学堂模拟赛d7t1 消失的数字
题目描述 现在,我的手上有 n 个数字,分别是 a1; a2; a3; :::; an.我现在需要删除其中的 k 个数字.当然我不希望随随便便删除,我希望删除 k个数字之后,剩下的 n - k 个数中 ...
- 巧克力棒&&新年的巧克力棒
巧克力棒 题目描述 LYK 找到了一根巧克力棒,但是这根巧克力棒太长了,LYK 无法一口吞进去.具体地,这根巧克力棒长为 n,它想将这根巧克力棒折成 n 段长为 1 的巧克力棒,然后慢慢享用.它打算每 ...
- Android平台Airplay的实现方法
Airplay属于局域网内异构设备之间分享多媒体数据的一种通信协议.Airplay设备有客户端和服务器之分,一般将小屏IOS设备实现为Airplay客户端,大屏幕设备实现为Airplay服务器.即iP ...
- cocos2d-x andriod, Box2D.h: No such file or directory
原文链接:r=47980">http://www.cocos2d-x.org/forums/6/topics/47503? r=47980 You need to include li ...
- cocos2d js ClippingNode 制作标题闪亮特效
1.效果图: 之前在<Android 高仿 IOS7 IPhone 解锁 Slide To Unlock>中制作了文字上闪亮移动的效果,这次我们来看下怎样在cocos2d js 中做出类似 ...
- ADS-B显示终端6.8
更新日志; 本次更新依旧主要注重是BUG修正. 1 改动鼠标移动时地图重绘的BUG 鼠标移动时地图即发生重绘,占用了CPU资源,修正后仅仅当鼠标点击选中对象或拖动对象时地图才会发生重绘.极大程度上节省 ...