题意:将指定的一段位置[m,n]的链表反置,返回链表头。

思路:主要麻烦在链表头,如果要从链表头就开始,比较特殊。

  目前用DFS实现,先找到m-1的位置,再找到n+1的位置,中间这段就是否要反置的,交给DFS解决,用个计数器来统计已经反置的个数即可。

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* DFS(ListNode* t,ListNode* far, int num)
{
if(num==) return far;
ListNode* tmp=t->next;
t->next=far;
return DFS(tmp , t, num-);
} ListNode* reverseBetween(ListNode* head, int m, int n) {
if(m==n) return head;
ListNode* t=head, *r=head; for(int i=; i<n; i++) r=r->next; //找到第n+1个
if(m==) return DFS(head, r, n-m+); for(int i=; i<m-; i++) t=t->next; //找到第m-1个
t->next=DFS(t->next, r, n-m+);
return head;
}
};

AC代码

LeetCode Reverse Linked List II 反置链表2的更多相关文章

  1. LeetCode Reverse Linked List (反置链表)

    题意: 将单恋表反转. 思路: 两种方法:迭代和递归. 递归 /** * Definition for singly-linked list. * struct ListNode { * int va ...

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

  3. [LeetCode] 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-> ...

  4. LeetCode OJ:Reverse Linked List II(反转链表II)

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...

  5. [Leetcode] Reverse linked list ii 反转链表

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given1->2 ...

  6. leetcode——Reverse Linked List II 选择链表中部分节点逆序(AC)

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1-> ...

  7. [LeetCode 92] Reverse Linked List II 翻转单链表II

    对于链表的问题,根据以往的经验一般都是要建一个dummy node,连上原链表的头结点,这样的话就算头结点变动了,我们还可以通过dummy->next来获得新链表的头结点.这道题的要求是只通过一 ...

  8. [LeetCode]92. Reverse Linked List II反转部分链表

    /* 重点还是反转链表 思路就是中间的反转,然后两头接上 */ public ListNode reverseBetween(ListNode head, int m, int n) { if (he ...

  9. [leetcode]Reverse Linked List II @ Python

    原题地址:https://oj.leetcode.com/problems/reverse-linked-list-ii/ 题意: Reverse a linked list from positio ...

随机推荐

  1. 分布式数据存储 - MySQL双主复制

    上篇文章<分布式数据存储 - MySQL主从复制>,我们说到MySQL主从复制很好的保障了从库,读的高可用性.so,问题来了: 1.针对主库,写的高可用性又是如何做到高可用性? 2.如果需 ...

  2. jvm性能调优---jstat的用法

    Jstat是JDK自带的一个轻量级小工具.全称“Java Virtual Machine statistics monitoring tool”,它位于java的bin目录下,主要利用JVM内建的指令 ...

  3. shell脚本执行查找进程,然后查杀进程

    shell 执行查找进程,然后查杀进程脚本如下: ps -ef | grep 'IOE' |grep -v 'grep'| awk '{print \$2}' |while read pid; do ...

  4. linux下tigervnc-servere服务的安装与使用

    关于tigervnc-servere的安装,可以直接使用本地yum源进行安装. [root@ ~]# yum install tigervnc-server -y 其中tigervnc的主要配置文件位 ...

  5. 用httpPost对JSON发送和接收的例子

    HTTPPost发送JSON: private static final String APPLICATION_JSON = "application/json";         ...

  6. servlet学习笔记一

    Servlet一.基本概念 我们的程序根据是否需要访问网络,可分为网络程序和非网络程序.而 网络程序又分为B/S结构和C/S结构. 什么是C/S?即客户端(Client)/服务器(Server)模式. ...

  7. Android核心分析之二十五Android GDI之共享缓冲区机制

    Androird GDI之共享缓冲区机制 1  native_handle_t对private_handle_t 的包裹     private_handle_t是gralloc.so使用的本地缓冲区 ...

  8. web.xml配置bug之提示The content of element type "web-app" must match "(icon?,display- name?,description?,distributable?,

    错误:配置web.xml时,出现红色叉叉,提示 The content of element type "web-app" must match "(icon?,disp ...

  9. asyn4j -- java 异步方法调用框架

    asyn4j 是一个java异步方法调用框架,基于消费者与生产者模式.包括了异步方法执行,异步回调执行,异步工作缓存模块.支持Spring. 让我们写异步方法不再写很多的相关多线程代码.用asyn4j ...

  10. IOS中延时执行的几种方式的比较

    本文列举了四种延时执行某函数的方法及其一些区别.假如延时1秒时间执行下面的方法. - (void)delayMethod { NSLog(@"execute"); } 1.perf ...