【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.
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.
思路:
首先把指针移动到第m个元素
然后计算需要交换的元素个数,n-m
每次交换时,把下一个元素交换到需要交换的初始位置
如
1->2->3->4->5->NULL
找到交换位置
1->->3->4->5->NULL
把下一个元素交换到需要交换的初始位置
1->->2->4->5->NULL
继续交换
1->->3->2->5->NULL
注意初始位置在开头时,每次交换都要更新head
/**
* 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 *p1=head;
ListNode *p1Pre=new ListNode(); int k=n-m; p1Pre->next=p1; ListNode *needDelete=p1Pre; while(m->)
{
p1Pre=p1;
p1=p1->next;
m--;
} while(k>)
{
ListNode* cur=p1->next;
p1->next=cur->next; if(p1Pre->next==head)
{
head=cur;
} cur->next=p1Pre->next;
p1Pre->next=cur; k--;
} delete needDelete;
return head;
}
};
【leetcode】Reverse Linked List II的更多相关文章
- 【leetcode】Reverse Linked List II (middle)
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(easy)
Reverse a singly linked list. 思路:没啥好说的.秒... ListNode* reverseList(ListNode* head) { ListNode * rList ...
- 【链表】 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- ...
- 【LeetCode92】Reverse Linked List II★★
题目描述: 解题思路: 题目大意:给定一个链表,反转第m到第n个结点部分,m.n满足1 ≤ m ≤ n ≤ length of list. 解题思路参照LeetCode206题,用迭代法,不过要注意以 ...
- 【LeetCode】链表 linked list(共34题)
[2]Add Two Numbers (2018年11月30日,第一次review,ko) 两个链表,代表两个整数的逆序,返回一个链表,代表两个整数相加和的逆序. Example: Input: ( ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- 【LeetCode】731. My Calendar II 解题报告(Python)
[LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- 【LeetCode】227. Basic Calculator II 解题报告(Python)
[LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
随机推荐
- 通过LVS+Keepalived搭建高可用的负载均衡集群系统
1. 安装LVS软件 (1)安装前准备操作系统:统一采用Centos6.5版本,地址规划如下: 服务器名 IP地址 网关 虚拟设备名 虚拟ip Director Server 192.168 ...
- python 多线程就这么简单
原文地址:http://www.cnblogs.com/fnng/p/3670789.html
- php self与static的区别
self vs static 用一个demo来直接说明self与static的区别.self示例: <?phpclass Vehicle { protected static $name ...
- Java读取txt文件,计算2011年9月份的通话时间
public class test2 { /** * @param args * @throws IOException */ public static void main(String[] arg ...
- asp.net mvc 4 高级编程学习笔记:第三章 视图(2)
页面布局 asp.net MVC中提供了布局的支持,默认情况下才布局文件保存到 /View/Shared/目录下的_Layout.cshtml,View目录有个_ViewStart.cshtml文件, ...
- 新浪微博客户端(35)-使用NSMutableAttributedString实现多行文本的效果
DJComposeViewController.m import "DJComposeViewController.h" #import "DJAccountTool.h ...
- editplus中使用emmet?
要用emmet生成html类型, 格式是: html:???, 意思是 都是html大类型, 小类型用冒号. 如:html:5, 或者全部都是! 则生成html5的类型文档. emmet是zen co ...
- TP3.1 中URL和APP区别
1.__URL__指当前模块地址,就是当前的action的地址.(每个__action都是一个模块) eg:当前打开config.html,那么config.html里边的__URL__/sav ...
- wordpress如何批量关闭旧日志留言功能
于一些wordpress技术博客或者其他wordpress博客来说,一些旧日志的内容可能已经过时了,但是一些读者,还是对一些问题“纠缠不清”或者“喋喋不休”,怎么办,把留言关了就好了: UPDATE ...
- CF #305(Div.2) D. Mike and Feet(数学推导)
D. Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard inp ...