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 ...
随机推荐
- 我的 FPGA 学习历程(07)—— BCD 编码:移位加 3 算法
2-10 进制码,也称为 BCD 码,它的编码方式则是通过一个 4 位二进制来表示一个 10 进制数,部分十进制对应的 BCD 码如下 十进制数 | BCD 码 13 --> 0001_0011 ...
- Openjudge — 7624 山区建小学
问题描述 政府在某山区修建了一条道路,恰好穿越总共m个村庄的每个村庄一次,没有回路或交叉,任意两个村庄只能通过这条路来往.已知任意两个相邻的村庄之间的距离为di(为正整数),其中,0 < i & ...
- PRTG测试!
http://www.paessler.com/prtg. 我的效果图:
- S7 Connection 通讯
参考两个链接: http://www.ad.siemens.com.cn/service/answer/solution.aspx?Q_ID=74626&cid=1029 https://su ...
- java课程之团队开发冲刺阶段1.1
一.今天所要完成的内容 1.实现软件添加日期的功能并生成当前所在周的功能 2.对之前的代码进行重新排版,将主函数的内容移到方法中 3.利用Android自带的左侧菜单栏实现app的整体美观
- go语言指针理解
- nmon监控分析
一.下载软件安装 wget http://sourceforge.net/projects/nmon/files/nmon_linux_14i.tar.gz tar xf nmon_linux_14i ...
- pycharm 中 django 导入静态文件不提示补全
File—>setting----->Languages & Frameworks ------> Python Template Languages ------> ...
- tensorflow-变量
#计数器 import tensorflow as tf state = tf.Variable(0,name='counter') #设定变量print(state.name) #打印变量one = ...
- C语言常用的编程规范
1排版 1-1相对独立的程序块之间.变量说明之后必须加空行. 示例:如下例子不符合规范. if (!valid_ni(ni)) { ... // program code } repssn_ind = ...