/**
* Source : https://oj.leetcode.com/problems/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->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 ≤ m ≤ n ≤ length of list.
*/
public class ReverseLinkedList2 { /**
*
* 反转单向链表指定范围内的元素
*
* 需要考虑第一个元素是否被翻转
*
* @param head
* @param m
* @param n
* @return
*/
public Node reverse (Node head, int m, int n) {
if (head == null) {
return head;
}
Node dummy = new Node();
dummy.next = head;
int pos = 1;
Node unreverseListLast = dummy;
Node reverseListLast = null;
Node cur = head;
Node next = null;
Node pre = dummy;
while (cur != null && pos <= n) {
next = cur.next;
if (pos == m) {
unreverseListLast = pre;
reverseListLast = cur;
} else if (pos > m) {
// 在制定范围内,反转
cur.next = pre;
}
pre = cur;
cur = next;
pos++;
} // 反转完指定范围内的元素,将反转部分和未反转部分连接起来
unreverseListLast.next = pre;
reverseListLast.next = cur; return dummy.next;
} private static class Node {
int value;
Node next; @Override
public String toString() {
return "Node{" +
"value=" + value +
", next=" + (next == null ? "" : next.value) +
'}';
}
} private static void print (Node node) {
while (node != null) {
System.out.println(node);
node = node.next;
}
System.out.println();
} public Node createList (int[] arr) {
if (arr.length == 0) {
return null;
}
Node head = new Node();
head.value = arr[0];
Node pointer = head;
for (int i = 1; i < arr.length; i++) {
Node node = new Node();
node.value = arr[i];
pointer.next = node;
pointer = pointer.next;
}
return head;
} public static void main(String[] args) {
ReverseLinkedList2 reverseLinkedList2 = new ReverseLinkedList2();
print(reverseLinkedList2.reverse(reverseLinkedList2.createList(new int[]{1,2,3,4,5}), 2, 4));
print(reverseLinkedList2.reverse(reverseLinkedList2.createList(new int[]{1,2,3,4,5}), 2, 2));
print(reverseLinkedList2.reverse(reverseLinkedList2.createList(new int[]{1,2,3,4,5}), 2, 5));
print(reverseLinkedList2.reverse(reverseLinkedList2.createList(new int[]{1,2,3,4,5}), 1, 5));
print(reverseLinkedList2.reverse(reverseLinkedList2.createList(new int[]{}), 1, 1));
} }

leetcode — reverse-linked-list-ii的更多相关文章

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

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

  3. [leetcode]Reverse Linked List II @ Python

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

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

  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 Reverse Linked List II 反置链表2

    题意:将指定的一段位置[m,n]的链表反置,返回链表头. 思路:主要麻烦在链表头,如果要从链表头就开始,比较特殊. 目前用DFS实现,先找到m-1的位置,再找到n+1的位置,中间这段就是否要反置的,交 ...

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

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

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

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

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

随机推荐

  1. Java IO 整理

    1.Java IO中的操作类之间的继承关系 2.在java中使用File类表示文件本身,可以直接使用此类完成文件的各种操作,如创建.删除 3.RandomAccessFile类可以从指定位置开始读取数 ...

  2. IntelliJ IDEA最新破解版2018.3.1(附2018.2.2 完美破解教程)

    2018.3.1最新版破解 1.官网下载IDEA 2018.3.1的商业版本点我去下载 2.破解jar下载:JetbrainsIdesCrack-3.4-release-enc.jar点我去下载 3. ...

  3. ios开发中的深拷贝和浅拷贝

    这是一个老生常谈的话题,面试中也经常被问到,下面总结一下自己的一些心得. 一句话总结: 浅拷贝就是指针拷贝: 深拷贝是对象本身的拷贝: 下面一张抽象的图可以直观的表述出两句话的内涵 其实这里还引申出了 ...

  4. eclipse中生成的html存在中文乱码问题的解决方法

    最近在做测试报告生成时遇到了个中文乱码的问题,虽然在html创建过程中设置了编码格式htmlReporter.config().setEncoding("UTF-8");但是生成的 ...

  5. FCC(ES6写法) Map the Debris

    返回一个数组,其内容是把原数组中对应元素的平均海拔转换成其对应的轨道周期. 原数组中会包含格式化的对象内容,像这样 {name: 'name', avgAlt: avgAlt}. 思路: 直接使用公式 ...

  6. [Swift]LeetCode83. 删除排序链表中的重复元素 | Remove Duplicates from Sorted List

    Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...

  7. [Swift]LeetCode330. 按要求补齐数组 | Patching Array

    Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...

  8. [Swift]LeetCode332. 重新安排行程 | Reconstruct Itinerary

    Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...

  9. [Swift]LeetCode784. 字母大小写全排列 | Letter Case Permutation

    Given a string S, we can transform every letter individually to be lowercase or uppercase to create ...

  10. Java运行原理及内存分析

    Java运行原理及内存分析 一.Java运行原理 二.Java内存分析