题目

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.

Note:
Given m, n satisfy the following condition:
1 ≤ mn ≤ length of list.

题解

经典的题目就是链表逆序啦,一般的链表逆序是让把链表从前到后都逆序,这个是给定了起始位置和结束位置,方法是一样的。

就是维护3个指针,startpoint,node1和node2。

startpoint永远指向需要开始reverse的点的前一个位置。

node1指向正序中第一个需要rever的node,node2指向正序中第二个需要reverse的node。

交换后,node1 在后,node2在前。这样整个链表就逆序好了。

代码如下:

 1     public ListNode reverseBetween(ListNode head, int m, int n) {
 2         ListNode newhead = new ListNode(-1);
 3         newhead.next = head;
 4         
 5         if(head==null||head.next==null)
 6             return newhead.next;
 7             
 8         ListNode startpoint = newhead;//startpoint指向需要开始reverse的前一个
 9         ListNode node1 = null;//需要reverse到后面去的节点
         ListNode node2 = null;//需要reverse到前面去的节点
         
         for (int i = 0; i < n; i++) {
             if (i < m-1){
                 startpoint = startpoint.next;//找真正的startpoint
             } else if (i == m-1) {//开始第一轮
                 node1 = startpoint.next;
                 node2 = node1.next;
             }else {
                 node1.next = node2.next;//node1交换到node2的后面
                 node2.next = startpoint.next;//node2交换到最开始
                 startpoint.next = node2;//node2作为新的点
                 node2 = node1.next;//node2回归到node1的下一个,继续遍历
             }
         }
         return newhead.next;
     }

Reverse Linked List II leetcode java的更多相关文章

  1. Reverse Linked List II——LeetCode

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

  2. Reverse Linked List II [LeetCode]

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

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

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

    92. 反转链表 II 92. Reverse Linked List II 题目描述 反转从位置 m 到 n 的链表.请使用一趟扫描完成反转. 说明: 1 ≤ m ≤ n ≤ 链表长度. LeetC ...

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

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

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

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

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

随机推荐

  1. 虚拟机zookeeper和hbase集群搭建

    集群zookeeper dataDir=/usr/local/zookeeper/dataDir dataLogDir=/usr/local/zookeeper/dataLogDir # the po ...

  2. RMQ_第一弹_Sparse Table

    title: RMQ_第一弹_Sparse Table date: 2018-09-21 21:33:45 tags: acm RMQ ST dp 数据结构 算法 categories: ACM 概述 ...

  3. Linux系统内存管理

    <linux 内存管理模型> 下面这个图将Linux内存管理基本上描述完了,但是显得有点复杂,接下来一部分一部分的解析. 内存管理系统可以分为两部分,分别是内核空间内存管理和用户空间内存管 ...

  4. android 消息机制,handler机制,messageQueue,looper

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha handler  就是 处理器 .  用来处理消息, 发送消息. handler   就 ...

  5. [BZOJ4487][JSOI2015]染色问题(容斥)

    一开始写了7个DP方程,然后意识到这种DP应该都会有一个通式. 三个条件:有色行数为n,有色列数为m,颜色数p,三维容斥原理仍然成立. 于是就是求:$\sum_{i=0}^{n}\sum_{j=0}^ ...

  6. bzoj 1006: [HNOI2008]神奇的国度 -- 弦图(最大势算法)

    1006: [HNOI2008]神奇的国度 Time Limit: 20 Sec  Memory Limit: 162 MB Description K国是一个热衷三角形的国度,连人的交往也只喜欢三角 ...

  7. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) A. Bear and Game 水题

    A. Bear and Game 题目连接: http://www.codeforces.com/contest/673/problem/A Description Bear Limak likes ...

  8. ROS知识(11)----同步两台机器时钟

    两台机器同时运行过程中,对于ROS的tf变换,其要求两台机器的时钟要保持一致. 1.查询时间 首先通过以下命令,看两台机器时钟是否有差异.在本机上,查看远程master的机器时间: ntpdate - ...

  9. Hyper-V创建固定大小虚拟机

    1.新建硬盘 点击确定,就创建好了一个固定大小的vhd文件,下面我们开始创建虚拟机. 2.创建虚拟机 输入虚拟机名称 选择第一代虚拟机 我这里给虚拟机分配512MB内存 网络配置 在这之前我们已经创建 ...

  10. java基础学习总结——面向对象2

    一.JAVA类的定义