1. Reverse Linked List II My Submissions QuestionEditorial Solution

    Total Accepted: 70579 Total Submissions: 252986 Difficulty: Medium

    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.

思路:

时间复杂度:O(n),空间复杂度O(1)

找到指定段元素,对该段元素逆转,同时保留指定段的两端的邻居

/**
* 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) {
assert(n>=m);
if(m==n)return head;
int i=1,j=1;
ListNode *p=head,*q=head,*prep=NULL;//保留指定段首元素的前一元素
while(p!=NULL&&i++<m){
if(i==m)prep=p;
p=p->next;
}
while(q!=NULL&&j++<n)q=q->next;/找到各自的最终位置,越界为空
if(p==NULL)return head; //起始位置越界,直接返回head
ListNode * tmp = p->next,*prenode = p,*qnext=q->next;
while(tmp!=qnext) //还未到达指定段的下一元素,则当前元素指向前一元素
{
ListNode *nextnode = tmp->next;
tmp->next = prenode;
prenode = tmp;
tmp = nextnode;
}
p->next = qnext;
if(prep!=NULL)prep->next = prenode;
else head = prenode;
return head;
}
};

49-Reverse Linked List II的更多相关文章

  1. 【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 ...

  2. 14. 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 ...

  3. 【原创】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 ...

  4. 92. 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- ...

  5. 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 ...

  6. 【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 ...

  7. [Linked List]Reverse Linked List,Reverse Linked List II

    一.Reverse Linked List  (M) Reverse Linked List II (M) Binary Tree Upside Down (E) Palindrome Linked ...

  8. LeetCode之“链表”:Reverse Linked List && Reverse Linked List II

    1. Reverse Linked List 题目链接 题目要求: Reverse a singly linked list. Hint: A linked list can be reversed ...

  9. 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- ...

  10. LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II

    1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...

随机推荐

  1. OO第四单元

    OO第四单元总结 第四单元架构设计 第一次作业 uml类图 这次作业我采取的基本思路就是根据指令来建造一个简易的类图,用于查询,其中umlclass中包含了umlAttraibute,umlOpera ...

  2. netty中使用protobuf实现多协议的消息

    在我们使用 netty 的过程中,有时候为了高效的传输数据,经常使用 protobuf 进行数据的传输,netty默认情况下为我们实现的 protobuf 的编解码,但是默认的只能实现单个对象的编解码 ...

  3. [火星补锅] 水题大战Vol.2 T1 && luogu P1904 天际线 题解 (线段树)

    前言: 当时考场上并没有想出来...后来也是看了题解才明白 解析: 大家(除了我)都知道,奇点和偶点会成对出现,而出现的前提就是建筑的高度突然发生变化.(这个性质挺重要的,我之前没看出来) 所以就可以 ...

  4. Machine learning(2-Linear regression with one variable )

    1.Model representation Our Training Set [训练集]: We will start with this ''Housing price prediction'' ...

  5. candy leetcode C++

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  6. 微服务之十四如何在 Ocelot 网关中配置多实例 Swagger 访问

    一.介绍 当我们开发基于微服务的应用程序的时候,有一个环节总是跳不过去的,那就是要创建 WebApi,然后,我们的应用程序基于 WebApi 接口去访问.在没有 Swagger 以前,我们开发好了 W ...

  7. 某企业桌面虚拟化项目-Citrix虚拟桌面解决方案

    xxx桌面虚拟化项目Citrix解决方案 xxx桌面虚拟化项目 Citrix解决方案 1 项目背景 秉承"尊重个性.创造价值.贡献于社会"的企业理念和开拓创新的精神,xxx所制造. ...

  8. 基于霸道秉火的STM32F103ZET6嵌入式开发之------定时器中断3

    1 #include "time.h" 2 #include "led.h" 3 #include "beep.h" 4 //¶¨Ê±Æ÷Ö ...

  9. mysql 免密码登录

    mysql 8免密码登录 UPDATE mysql.user SET authentication_string=null WHERE User='root'; FLUSH PRIVILEGES; e ...

  10. HCNP Routing&Switching之BGP团体属性和团体属性过滤器

    前文我们了解了BGP的路由过滤已经as-path过滤器的使用相关话题,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/15542559.html:今天我们来聊一聊 ...