【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->2->3->4->5->NULL, m = 2 and n = 4,
return 1->4->3->2->5->NULL.
思路:
好困啊,脑子晕晕的。 转了半天AC了。但写的很罗嗦,要学习大神的写法。 注意翻转的写法。
用伪头部
大神14行简洁代码
ListNode *reverseBetween(ListNode *head, int m, int n) {
if(m==n)return head;
n-=m;
ListNode prehead();
prehead.next=head;
ListNode* pre=&prehead;
while(--m)pre=pre->next;
ListNode* pstart=pre->next;
while(n--)
{
ListNode *p=pstart->next;
pstart->next=p->next;
p->next=pre->next;
pre->next=p;
}
return prehead.next;
}
我的繁琐代码
ListNode *reverseBetween(ListNode *head, int m, int n) {
ListNode fakehead();
ListNode * p = &fakehead;
for(int i = ; i < m; i++)
{
p = p->next = head;
head = head->next;
}
p->next = NULL; //m前的那一节末尾
ListNode *ptail = head; //翻转那一段的尾巴
ListNode *p1 = head, *p2 = NULL, *p3 = NULL;
if(p1->next != NULL)
{
p2 = p1->next;
}
p1->next = NULL;
for(int i = m; i < n; i++)
{
p3 = p2->next;
p2->next = p1;
p1 = p2;
p2 = p3;
}
p->next = p1;
ptail->next = p2;
return fakehead.next;
}
【leetcode】Reverse Linked List II (middle)的更多相关文章
- 【leetcode】Path Sum I & II(middle)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- 【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 ...
- 【leetcode】Reverse Nodes in k-Group (hard)☆
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- 【leetcode】Remove Linked List Elements(easy)
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- leetcode 之Reverse Linked List II(15)
这题用需要非常细心,用头插法移动需要考虑先移动哪个,只需三个指针即可. ListNode *reverseList(ListNode *head, int m, int n) { ListNode d ...
- 【leetcode】Swap Nodes in Pairs (middle)
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- 【leetcode】Search for a Range(middle)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- 【leetcode】Binary Search Tree Iterator(middle)
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- 【leetcode】Validate Binary Search Tree(middle)
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
随机推荐
- 详细介绍如何在win7下首次实现通过Git bash向Github提交项目
详细介绍如何在win7下首次实现通过Git bash向Github提交项目 引自:http://jingpin.jikexueyuan.com/article/35944.html 作者: wddoe ...
- glusterFS的常用命令 (转)
1. 启动/关闭/查看glusterd服务 # /etc/init.d/glusterd start # /etc/init.d/glusterd stop # /etc/init.d/g ...
- 天翼宽带政企网关B2-1P 如何获得超级管理员账号?
RT 用useradmin没办法做NAT,想进telecomadmin里面看看,,,,,并且已经使用过nE7jA%5m这个密码登录,没有用! 求办法!!! 最佳答案 查找超级管理员密码方法: 1.用光 ...
- C++之通用队列模版
功能:在qtcreator上用模版的方式实现队列数据格式存储多样化. 模版实现的两种方式: 1.模版的实现直接在头文件里声明并定义,别的类通过#include"*.h"进行包含,则 ...
- Redhat EL安装curses
1.下载curses安装包 http://ftp.gnu.org/pub/gnu/ncurses/ncurses-5.6.tar.gz2. tar -zxvf nurses-5.6.tar.gz 3 ...
- 【转】php 下载保存文件保存到本地的两种实现方法
来源:http://www.jb51.net/article/40485.htm 第一种: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <?php function d ...
- MongoDB 基础知识
一. 基础知识 1. MongoDB是一个文档型的数据库,文档就是一个键值对的有序集合. 例如这样:{"greeting":"hello world"} 2. ...
- 剑指Offer 树的子结构
题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) 思路: 分为2个部分.1先找出A中和B根节点相同的节点r. 2,咱判断B中所有孩子节点是不 ...
- u-boot 流程分析
u-boot 介绍: 对于计算机来说 , 从一开始上机通电是无法直接启动操作系统的 , 这中间需要一个引导过程 , 嵌入式Linux系统同样离不开引导程序 , 这个启动程序就叫启动加载程序(Boot ...
- 试用vSphere 6(三):安装vCenter 6(独立数据库)之:vCenter安装与配置
------------------------------------------ 一.VMware vSphere 6(RC版)安装配置系列文章: 1.试用vSphere 6(一):安装ESXi ...