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- ...
随机推荐
- Cheatsheet: 2013 09.01 ~ 09.09
.NET Multi Threaded WebScraping in CSharpDotNetTech .NET Asynchronous Patterns An Overview of Projec ...
- ggplot2 legend图例的修改
ggplot2中的legend包括四个部分: legend.tittle, legend.text, legend.key, legend.backgroud.针对每一部分有四种处理方式: eleme ...
- Ubuntu Linux 下文件名乱码(无效的编码)的快速解决办法
文件是在WIndows 下创建的,Windows 的文件名中文编码默认为GBK,而Linux中默认文件名编码为UTF8,由于编码不一致所以导致了文件名乱码的问题,解决这个问题需要对文件名进行转码.文件 ...
- Timeout expired超时时间已到. 达到了最大池大小 错误及Max Pool Size设置
此文章非原创,仅为分享.学习!!! 参考数据库链接串: <add key="data" value="server=192.168.1.123; port=3306 ...
- [SAP ABAP开发技术总结]BAPI调用
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- ARC 下处理内存暴涨的一个解决办法
有一种情况: ; i < ; i++) { NSString *s = @"ABC"; s = [s lowercaseString]; s = [s stringByApp ...
- hdu 1116 Play on Words 欧拉路径+并查集
Play on Words Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- h5上滑刷新(分页)
$('.dom').append('<div class="loadingwrap" id="loading" style="display:n ...
- jQuery的.click,.bind,.unbind,.on,.off,.delegate,.undelegate
.click与.bind .click和.bind都是给每个元素绑定事件,对于只绑定一个click事件,.bind事件的简写就是.click那种方式. 这两种方式都会出现两个问题: 第一个问题,如果要 ...
- 差之毫厘谬之千里!带你认识CPU后缀含义
intel的几代CPU中,后缀字母主要有以下几种: M:笔记本专用CPU,一般为双核,M前面一位数字是0,意味着是标准电压处理器,如果是7,则是低电压处理器. U:笔记本专用低电压CPU,一般为双核, ...