[Linked List]Reverse Linked List,Reverse Linked List II
/**
* 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 *p=head,*newHead=NULL,*next=NULL;
while(p){
next = p->next;
p->next = newHead;
newHead = p;
p = next;
}
return newHead;
}
};
Reverse a linked list from position m to n. Do it in-place and in one-pass.
For example:
Given 1->2->3->4->5->NULL, m = 2 and n = 4,
return 1->4->3->2->5->NULL.
Note:
Given m, n satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.
/**
* 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* p = head,*newHead = NULL,*next = NULL;
while(p){
next = p->next;
p->next = newHead;
newHead = p;
p = next;
}
return newHead;
}
ListNode* reverseBetween(ListNode* head, int m, int n) {
if(m==n){
return head;
}
ListNode *m_pre=NULL,*n_next=NULL,*tail=NULL,*cur=head;
int cnt = ;
while(cur){//找m的前驱
++cnt;
if(cnt == m){
tail = cur;//反转之前的头,反转之后的尾
break;
}
m_pre = cur;
cur=cur->next;
}
while(cur){//找n的后继
n_next = cur->next;
if(cnt == n){
break;
}
cur = n_next;
++cnt;
}
if(cur){
cur->next = NULL;
}
ListNode *newHead = reverseList(tail);
if(m_pre){
m_pre->next = newHead;
}else{
head = newHead==NULL? head:newHead;
}
if(tail){
tail->next = n_next;
}
return head;
}
};
[Linked List]Reverse Linked List,Reverse Linked List II的更多相关文章
- REVERSE关键字之REVERSE索引
昨天说到REVERSE关键字可以指REVERSE函数和REVERSE索引,简单介绍了下REVERSE函数的含义,今天简单整理下REVERSE索引. REVERSE索引也是一种B树索引,但它物理上将按照 ...
- [Linked List]Delete Node in a Linked List
otal Accepted: 48115 Total Submissions: 109291 Difficulty: Easy Write a function to delete a node (e ...
- Linked List-237. Delete Node in a Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- [LeetCode] 344 Reverse String && 541 Reverse String II
原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...
- (reverse) Text Reverse hdu1062
Text Reverse Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- 攻防世界 reverse 进阶 10 Reverse Box
攻防世界中此题信息未给全,题目来源为[TWCTF-2016:Reverse] Reverse Box 网上有很多wp是使用gdb脚本,这里找到一个本地还原关键算法,然后再爆破的 https://www ...
- [LeetCode] Reverse Linked List
Reverse a singly linked list. 这题因为palindrome linked list 的时候需要就顺便做了一下.利用三个指针:prev, now, next 相互倒腾就行. ...
- [LintCode] Reverse Linked List 倒置链表
Reverse a linked list. Have you met this question in a real interview? Yes Example For linked list 1 ...
- Reverse Linked List I&&II——数据结构课上的一道题(经典必做题)
Reverse Linked List I Question Solution Reverse a singly linked list. Reverse Linked List I 设置三个指针即可 ...
- 206. Reverse Linked List【easy】
206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...
随机推荐
- mysql 服务不见了的解决办法
昨天打开电脑mysql突然连接不了了,去服务里找,却找不到mysql服务了 解决:5.0版本:开始->运行->cmd,进到mysql安装的bin目录D:\MySQL\bin>mysq ...
- iOS获取文件路径
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px "PingFang SC"; color: #008400 } p.p2 ...
- ProxyCreationEnabled 和 LazyLoadingEnabled 的关系
我们会使用这两个设置来决定是否加载导航属性.默认情况这两个值都是true的,也就是说会以延迟加载的方式加载导航属性,也就是当我们访问导航属性的时候才会去查数据库获取导航属性的数据.db.Configu ...
- SQLServer服务器数据库之间的数据操作(完整版)
分类: 数据库开发技术 ---------------------------------------------------------------------------------- -- Au ...
- css基础之 id和选择器
id 和 class 选择器 如果你要在HTML元素中设置CSS样式,你需要在元素中设置"id" 和 "class"选择器. (1) id 选择器 id 选择器 ...
- Git学习笔记:Git基础
一.Git与其他版本控制系统的差别 Git 只关心文件数据的整体是否发生变化,而大多数其他系统则只关心文件内容的具体差异.这类系统每次记录有哪些文件作了更新,以及都更新了哪些行的什么内容.如下图,其他 ...
- LaTeX中titlesec宏包的使用
在 xelatex 中使用 \usepackage 指令使用 titlesec 宏包时,可以指定一些格式选项,如下: \usepackage[center]{titlesec} 其中 center 可 ...
- 2014第7周四excel多列文本复制技巧
刚才win8.1强制安装更新后重启,然后一直显示“安装更新失败正在,正在撤销更改,请不要关闭计算机”,等了很久还是不行,我还是强制按下了电源按钮,然后再次开机还是这样,实在没办法只能等,过了N久后没想 ...
- linux下so动态库一些不为人知的秘密(中二)
继续上一篇< linux下so动态库一些不为人知的秘密(中) >介绍so搜索路径,还有一个类似于-path,叫LD_RUN_PATH环境变量, 它也是把路径编译进可执行文件内,不同的是它只 ...
- Bayesian Formulation on Cooperative Tracking
Suppose a joint state representing a set of \(N_{n}\) nodes moving in a field\[ \textbf{X}= \b ...