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 ...
随机推荐
- c++ a+b
#include<iostream> using namespace std; int main() { int a,b,sum; cin>>a>>b; sum=a ...
- margin相关属性值
1.图片与文字对齐问题 图片与文字默认是居底对齐.一般img标签打头的小图标与文字对齐的话,通过 img{margin:0 3px -3px 0;} 这个的东西,能实现效果和兼容性俱佳的对齐效果: d ...
- Javascript Read Excel
本文引用以下路径 https://www.cnblogs.com/liuxianan/p/js-excel.html
- 使用Skaffold一键将项目发布到Kubernetes
当前skaffold版本为v0.4,还未发布正式版本,不建议在生产环境中使用: skaffold用于开发人员快速部署程序到Kubernetes中:skaffold提供了dev.run两种模式:使用sk ...
- Visual Studio(VS)秘钥集合
Visual Studio 2019 Pro :NYWVH-HT4XC-R2WYW-9Y3CM-X4V3Y
- sql 随机获取数据
SQL Server: SELECT TOP 10 * FROM T_USER ORDER BY NEWID() ORACLE: SELECT * FROM (SELECT * FROM T_USER ...
- aarch64的架构:unrecognized command line option '-mfpu=neon'
不用添加这个'-mfpu=neon'的编译器选项了,因为这个架构下neon是默认启动的. 参考: https://lists.linaro.org/pipermail/linaro-toolchain ...
- LoadRunner(一)——性能测试基础及性能指标概述
参考学习感谢:<精通软件性能测试与LoadRunner实战> 一.典型的性能测试场景 某个产品要发布了,需要对全市的用户做集中培训.通常在进行培训的时候,老师讲解完成一个业务以后,被培训用 ...
- jq 点击复制div里面的内容 如果粘贴到富文本中,会将样式,里面所有的标签,文字一并粘贴进去
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- c++ 获取磁盘句柄
磁盘的句柄可以用CreateFile函数获得.获得句柄后,就可以配合其他函数对磁盘进行一些操作. int main() { HANDLE hFile = INVALID_HANDLE_VALUE; h ...