leetcode — reverse-linked-list-ii
/**
* 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的更多相关文章
- 【原创】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]Reverse Linked List II @ Python
原题地址:https://oj.leetcode.com/problems/reverse-linked-list-ii/ 题意: Reverse a linked list from positio ...
- [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] 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 Reverse Linked List II 反置链表2
题意:将指定的一段位置[m,n]的链表反置,返回链表头. 思路:主要麻烦在链表头,如果要从链表头就开始,比较特殊. 目前用DFS实现,先找到m-1的位置,再找到n+1的位置,中间这段就是否要反置的,交 ...
- 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 ...
- LeetCode之“链表”:Reverse Linked List && Reverse Linked List II
1. Reverse Linked List 题目链接 题目要求: Reverse a singly linked list. Hint: A linked list can be reversed ...
- LeetCode 92. 反转链表 II(Reverse Linked List II)
92. 反转链表 II 92. Reverse Linked List II 题目描述 反转从位置 m 到 n 的链表.请使用一趟扫描完成反转. 说明: 1 ≤ m ≤ n ≤ 链表长度. LeetC ...
随机推荐
- Linux Kernel C语言编程范式
介绍 不同的编程语言具有不同的抽象原语(如下),有的原语抽象层次低,有的原语抽象层次高.其中函数式.DSL是这几年十分热门的编程语言概念. 过程式抽象原语:变量 对象式抽象原语:对象 函数式抽象原语: ...
- MT7688交叉编译环境配置
在ubuntu下设置MT7688交叉编译环境,用于编译mt7688下使用的程序 1.首先在vmware下安装ubuntu64位,由于交叉编译工具需要64位系统,此次安装的是ubuntu14 2.在ub ...
- web 基础设置
1.设置代码格式为UTF-8 2.运行jsp文档 3.设置自己喜欢的浏览器运行,设置为默认的 找到自己的浏览器位置 点ok Name是名字的意思 为这个浏览器娶一个名字 是什么浏览器就写什么名字 4. ...
- PageHelper分页插件的使用
大家好!今天写ssm项目实现分页的时候用到pageHelper分页插件,在使用过程中出现了一些错误,因此写篇随笔记录下整个过程 1.背景:在项目的开发的过程中,为了实现所有的功能. 2.目标:实现分页 ...
- cadence焊盘及元件封装制作
前面学习了元件封装的制作,由于琐碎事情的耽误,加上学习python,没有及时的总结这部分内容,现在做一个补充!
- Winform将一个窗体显示在另一个窗体中
private void ShowForm(Form Indexform) { Form1 form1 = new Form1(); form1 .TopLevel = false; form1 .P ...
- CFUpdate高速模式下出现Error #2038提示的解决方案
使用CFUpdate上传文件,在IE模式下是正常的,切换到高速模式下出现提示Error #2038错误,文件无法上传. 向作者了解到需要设置challs_flash_update函数中的a.url为绝 ...
- 格式化数据保留两位小数,输入格式为 :xxx,xx,,,,x,,(x为浮点数)
/** * 格式化字符串 */ static String dataFormat(String data){ String formatedData = ""; // 浮点数正则表 ...
- ASP.NET 4.0验证请求 System.Web.HttpRequestValidationException: A potentially dangerous Request.F
System.Web.HttpRequestValidationException: A potentially dangerous Request.F 在使用类似eWebedtior 拷贝内容进去的 ...
- Java作业五(2017-10-15)
/*3-6.程序员;龚猛*/ 1 package zhenshu; import java.util.Scanner; public class text { public static void m ...