Reverse Linked List II [LeetCode]
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.
Summary: First writes down the reverse parts (m to n) , then handles nodes before the m-th node and after the n-th node.
class Solution {
public:
ListNode *reverseBetween(ListNode *head, int m, int n) {
ListNode * current = head;
ListNode * pre_m_node = NULL;
ListNode * new_head = NULL;
int i = ;
while(current != NULL) {
if(i == m -)
break;
pre_m_node = current;
i ++;
current = current -> next;
}
//reverse m to n
ListNode * pre_n_node = current;
int num_of_reverse_op = ;
int num_of_nodes_to_reverse = n - m + ;
while(num_of_reverse_op < num_of_nodes_to_reverse) {
ListNode * pre_head = new_head;
new_head = current;
current = current -> next;
num_of_reverse_op ++;
new_head -> next = pre_head;
}
//connect rest node after the nth node
pre_n_node -> next = current; if(pre_m_node != NULL) {
pre_m_node -> next = new_head;
return head;
} else {
return new_head;
}
}
};
Reverse Linked List II [LeetCode]的更多相关文章
- Reverse Linked List II——LeetCode
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- Reverse Linked List II leetcode java
题目: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1 ...
- lc面试准备:Reverse Linked List II
1 题目 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 ...
- LeetCode 92. 反转链表 II(Reverse Linked List II)
92. 反转链表 II 92. Reverse Linked List II 题目描述 反转从位置 m 到 n 的链表.请使用一趟扫描完成反转. 说明: 1 ≤ m ≤ n ≤ 链表长度. LeetC ...
- 【leetcode】Reverse Linked List II
Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...
- 【原创】Leetcode -- Reverse Linked List II -- 代码随笔(备忘)
题目:Reverse Linked List II 题意:Reverse a linked list from position m to n. Do it in-place and in one-p ...
- 【LeetCode练习题】Reverse Linked List II
Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...
- leetcode -day30 Reverse Linked List II
1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one- ...
随机推荐
- Common Macros for Build Commands and Properties
https://msdn.microsoft.com/en-us/library/c02as0cs.aspx $(ProjectDir) The directory of the proje ...
- How to evaluate a transimpedance amplifier (part 2)
In my previous blog on "How to evaluate a transimpedance amplifier, part 1", we looked at ...
- 数据词典与ABAP类型映射
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- CUBRID学习笔记 39 net使用dataset 返回查询的数据
using CUBRID.Data.CUBRIDClient; namespace DataSetExample { class Program { static ...
- poj 1064 (二分+控制精度) && hdu 1551
链接:http://poj.org/problem?id=1064 Cable master Time Limit: 1000MS Memory Limit: 10000K Total Submi ...
- hdu 1023 卡特兰数+高精度
Train Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- bzoj 2957: 楼房重建 线段树
2957: 楼房重建 Time Limit: 10 Sec Memory Limit: 256 MB[Submit][Status][Discuss] Description 小A的楼房外有一大片施 ...
- HDU-4511 小明系列故事——女友的考验 floyd变种-标号递增最短路
题意:给定N个点,现在要求出从1号点到N号点的最短路.题目给的限制条件就是对于某条路径是不能够走的,但是可以选择某段路径走,另外就是所走的路径的标号必须是递增的. 分析:由于给定的是一些列的坐标点,这 ...
- 装了maven插件的eclipse中M2_REPO无法编辑、删除(转)
今天用了新版本的eclipse,用maven在命令行生成了一个普通项目.导入eclipse之后发现本地仓库的路径不正确. 显示的为 user.path/.m2/repository 但是我的仓库早已经 ...
- SQL.变量、运算符、if、while
变量: SQL语言也跟其他编程语言一样,拥有变量.分支.循环等控制语句. 在SQL语言里面把变量分为局部变量和全局变量,全局变量又称系统变量. 局部变量: 使用declare关键字给变量声明,语法非常 ...