36-翻转链表 II

翻转链表中第m个节点到第n个节点的部分

注意事项

m,n满足1 ≤ m ≤ n ≤ 链表长度

样例

给出链表1->2->3->4->5->null, m = 2 和n = 4,返回1->4->3->2->5->null

挑战

在原地一次翻转完成

标签

链表

思路

借助2个空的节点,置于链表头部,一次遍历链表,找到翻转子链尾部subEnd,不翻转子链尾部lastEnd,头部nextHead,以及当前节点nowNode,在遍历时,将nowNode插入lastEnd与subEnd之间。最后,将翻转子链与nextHead相连。

code

  1. /**
  2. * Definition of singly-linked-list:
  3. *
  4. * class ListNode {
  5. * public:
  6. * int val;
  7. * ListNode *next;
  8. * ListNode(int val) {
  9. * this->val = val;
  10. * this->next = NULL;
  11. * }
  12. * }
  13. */
  14. class Solution {
  15. public:
  16. /**
  17. * @param head: The head of linked list.
  18. * @param m: The start position need to reverse.
  19. * @param n: The end position need to reverse.
  20. * @return: The new head of partial reversed linked list.
  21. */
  22. ListNode *reverseBetween(ListNode *head, int m, int n) {
  23. // write your code here
  24. ListNode newHead1(-1), newHead2(-1);
  25. newHead1.next = &newHead2;
  26. newHead2.next = head;
  27. ListNode *nowNode=head, *subHead=head, *subEnd=NULL, *lastEnd=&newHead1, *nextHead=NULL;
  28. int i=1;
  29. while(nowNode != NULL) {
  30. if(i <= m) {
  31. lastEnd = lastEnd->next;
  32. subEnd = nowNode;
  33. nextHead = nowNode;
  34. nowNode = nowNode->next;
  35. i++;
  36. }
  37. else if(i <= n) {
  38. ListNode *temp = nowNode->next;
  39. nextHead->next = NULL;
  40. lastEnd->next = nowNode;
  41. lastEnd->next->next = subEnd;
  42. subEnd = nowNode;
  43. nowNode = temp;
  44. i++;
  45. }
  46. else {
  47. nextHead->next = nowNode;
  48. break;
  49. }
  50. }
  51. if(m == 1) {
  52. return lastEnd->next;
  53. }
  54. else {
  55. return head;
  56. }
  57. }
  58. };

lintcode-36-翻转链表 II的更多相关文章

  1. lintcode 中等题: reverse linked list II 翻转链表II

    题目 翻转链表 II 翻转链表中第m个节点到第n个节点的部分 样例 给出链表1->2->3->4->5->null, m = 2 和n = 4,返回1->4-> ...

  2. lintcode: 翻转链表

    题目: 翻转链表 翻转一个链表 样例 给出一个链表1->2->3->null,这个翻转后的链表为3->2->1->null 挑战 在原地一次翻转完成 解题: 递归还 ...

  3. 92. Reverse Linked List II 翻转链表II

    Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...

  4. LeetCode 92 | 大公司常考的面试题,翻转链表当中指定部分

    今天是LeetCode专题的第58篇文章,我们一起来看看LeetCode 92题,翻转链表II(Reverse LInked List II). 这题的官方难度是Medium,2451个赞同,145个 ...

  5. [LintCode] Reverse Nodes in k-Group 每k个一组翻转链表

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...

  6. [LeetCode系列]翻转链表问题II

    给定一个链表和两个整数m, n, 翻转链表第m个节点到第n个节点(从1开始计数). 如, 给定链表: 1->2->3->4->5->NULL, 以及 m = 2, n = ...

  7. [LeetCode] Reverse Nodes in k-Group 每k个一组翻转链表

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...

  8. C语言递归,非递归实现翻转链表

    翻转链表作为,链表的常用操作,也是面试常遇到的. 分析非递归分析: 非递归用的小技巧比较多,很容易出错. 递归分析比较简单,在代码里面 代码: #include<stdio.h> #inc ...

  9. 025k个一组翻转链表

    #include "000库函数.h" struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), n ...

随机推荐

  1. 帝国CMS如何禁止内容关键字替换ALT和title中的关键词为链接

    很多帝国cms用户喜欢使用关键字替换来实现文章自动内链的方法. 为什么要用关键词替换功能呢?这关系到站内优化,下面直接进入正题. 解决办法:打开e/class/functions.php 查找 '/' ...

  2. IAP笔记

    1)   首先是IAP BootLoader程序设置:根据common.h里面的宏定义,设置BootLoader所占用的Flash空间. 2)   设置IAP UserApp程序设置:该型号Flash ...

  3. python学习——复习

    一.基础知识: 1.文件操作有哪些模式?请简述各模式的作用. 'r' 读模式,相应的方法有 read(),readline(),readlines() 'w' 写模式,相应的方法有 write(),w ...

  4. python教程(二)·循环语句

    计算机程序中常常需要重复执行某些语句,我们总不能将同一语句写上百遍吧?所以在python中,当然其它计算机语言也是,有一种语句可以重复执行相同的操作,这种语句就是 "循环语句",而 ...

  5. C程序设计语言笔记-第一章

     The C Programming language notes 一 基础变量类型.运算符和判断循环         char                 字符型  character      ...

  6. (杭电2053)A + B Again(转换说明符)

    Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): ...

  7. centos7关闭图形界面启动系统

    手动敲那么多不累么?仅2条命令(好) 1,命令模式systemctl set-default multi-user.target 2,图形模式systemctl set-default graphic ...

  8. Fedora 下面安装FTP服务

    1. yum install vsftpd 2. systemctl disable vsftpd.service 3. systemctl stop vsftpd.service 4. system ...

  9. Chip-seq peak annontation

    Chip-seq peak annontation Chip-seq peak annontation PeRl narrowPeak/boardPeak narrowPeak/boardPeak 是 ...

  10. 笔记-django第一个项目

    笔记-django第一个项目 1.      创建项目 安装 Django 之后,现在有了可用的管理工具 django-admin.可以使用 django-admin 来创建一个项目: 看下djang ...