LeetCode Reverse Linked List II 反置链表2
题意:将指定的一段位置[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的更多相关文章
- LeetCode Reverse Linked List (反置链表)
题意: 将单恋表反转. 思路: 两种方法:迭代和递归. 递归 /** * Definition for singly-linked list. * struct ListNode { * int va ...
- 【原创】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 a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- 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-> ...
- [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 ...
- 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-> ...
- [LeetCode 92] Reverse Linked List II 翻转单链表II
对于链表的问题,根据以往的经验一般都是要建一个dummy node,连上原链表的头结点,这样的话就算头结点变动了,我们还可以通过dummy->next来获得新链表的头结点.这道题的要求是只通过一 ...
- [LeetCode]92. Reverse Linked List II反转部分链表
/* 重点还是反转链表 思路就是中间的反转,然后两头接上 */ public ListNode reverseBetween(ListNode head, int m, int n) { if (he ...
- [leetcode]Reverse Linked List II @ Python
原题地址:https://oj.leetcode.com/problems/reverse-linked-list-ii/ 题意: Reverse a linked list from positio ...
随机推荐
- HDU 1465 不容易系列之一(错排,递归)
简而言之,就是把n个信封全部装错的可能数.(中问题,具体看题目) //当n个编号元素放在n个编号位置,元素编号与位置编号各不对应的方法数用M(n)表示, //那么M(n-1)就表示n-1个编号元素放在 ...
- linux使用crontab实现PHP执行定时任务及codeiginter参数传递相关
http://www.phpddt.com/php/linux-crontab.html crontab: yum install crontabs //安装 说明: /sbin/service cr ...
- **app后端设计(10)--数据增量更新(省流量)
在新浪微博的app中,从别的页面进入主页,在没有网络的情况下,首页中的已经收到的微博还是能显示的,这显然是把相关的数据存储在app本地. 使用数据的app本地存储,能减少网络的流量,同时极大提高了用户 ...
- SOLID 原则
世纪的前几年里,“ Uncle Bob”Robert Martin 引入了用OOP 开发软件的五条原 则,其目的是设计出更易于维护的高质量系统.无论是设计新应用程序,还是重构现有基 本代码,这些 S ...
- Gvim for php 安装配置
VIM for PHP Windows 2011-05-14 11:51:51| 分类: Php|举报|字号 订阅 虽然vim本质上只是一个编辑器.但只要配合一些适当的插件,vim也能变成一 ...
- [转]fedora启动telnet服务
http://blog.chinaunix.net/uid-22996709-id-3056078.html 在win7上安装了SecurityCRT,登录VMWARE Fedora时候登录超时,检查 ...
- linux下PostgreSQL数据库的源码安装
实验环境>>>>>>>>>>>>>>>>>>操作系统:CentOS release 6.3 ...
- 执行脚本出现bin/bash: bad interpreter: No such file or directory
-bash: ./test.sh: /bin/bash^M: bad interpreter: No such file or directory VI打开文件,没发现任何问题, 把/bin/bash ...
- getElementByClassName封装函数用法
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- WordPress主题制作教程1:文件构成
在最简单的情况下,一个WordPress主题由两个文件构成: index.php ------------------主模版 style.css -------------------主样式表 以下 ...