[分析]
使用递归会Memory Limit Exceed,迭代方式参考
https://leetcode.com/discuss/17483/share-my-java-solution-with-comments-in-line
解析见代码注释
Reverse Linked List, Reverse Linked List II 可使用此题的解题技巧,可联系着体会,而Reorder List中包含Reverse Linked List步骤。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
// prev->head->...->...->tail->|...| //init state of reversing k group [head, tail]
// prev->tmp->...->...->tail->|...|
// prev->...->...->tail->tmp->|...| //delete tmp & insert at tail.next
// prev->...->tail->tmp'->...->head->|...| //keep doing until prev.next = tail
// prev->tail->...->...->head->|...| // finish state
public ListNode reverseKGroup(ListNode head, int k) {
if (head == null || head.next == null || k < 2)
return head;
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode prev = dummy, tail = head, tmp;
while (true) {
// check remain length of list
int count = 0;
while (tail != null) {
count++;
if (count == k) break;
tail = tail.next;
}
if (tail == null) break;
// reverse a K group
head = prev.next;
while (prev.next != tail) {
tmp = prev.next;
prev.next = tmp.next; // delete tmp from prev.next
tmp.next = tail.next; // step 1 of inserting tmp at tail.next
tail.next = tmp; // step 2 of inserting tmp at tail.next
}
prev = head;
tail = head.next;
}
return dummy.next;
}
}
public class Solution {
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null)
return head;
ListNode curr = head.next;
head.next = null;
ListNode prev = head, next;
while (curr != null) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
public ListNode reverseList1(ListNode head) {
if (head == null || head.next == null)
return head;
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode tail = head;
while (tail.next != null) {
tail = tail.next;
}
ListNode tmp;
while (dummy.next != tail) {
tmp = dummy.next;
dummy.next = tmp.next;
tmp.next = tail.next;
tail.next = tmp;
}
return tail;
}
}
public class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
int count = 1;
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode prev = dummy; //prev is the (m-1)th node
while (count < m) {
prev = prev.next;
count++;
}
ListNode tail = prev.next;
while (count < n) { // tail is the nth node
tail = tail.next;
count++;
}
ListNode tmp;
while (prev.next != tail) {
tmp = prev.next;
prev.next = tmp.next;
tmp.next = tail.next;
tail.next = tmp;
}
return dummy.next;
}
}
分享到:
相关推荐
《leetcode-solutions》,刷算法题,需要有一定的英文阅读能力。。。
LeetCode-Solutions-in-Swift.zip,swift 5中的leetcode解决方案
Algorithm-LeetCode-Sol-Res.zip,干净,易懂的解决方案和资源,为leetcode在线判断算法问题。,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
IDEA 插件,lettcode刷题,leetcode-editor7.4版本下载进行本地导入(直接将压缩包拖进IDEA即可)
Algorithm-leetcode-spider.zip,leetcode公司,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
leetcode 答案解析 golang解答
leetcode-cli-plugins leetcode-cli 的第 3 方插件。 什么是 如何使用 如何使用 插件 名称 描述 增强的命令 按公司或标签过滤问题 list 不要在同一台计算机上使 Chrome 的会话过期 login 不要在同一台计算机上使 ...
在IDE中解决LeetCode问题,支持leetcode.com与leetcode-cn.com,满足基本的做题需求。 理论上支持: IntelliJ IDEA PhpStorm WebStorm PyCharm RubyMine AppCode CLion GoLand DataGrip Rider MPS Android Studio。
解题思路思路和LeetCode-python 503.下一个更大元素 II一致,只是这里求的是下标的距离,而不是数值倒序搜索,用到栈,栈里存储索引情况1:若栈为
示例输入:输出: 1->1->2->3->4->4->5->6解题思路将k个链表建立成一个最小堆,再从堆顶pop()出每一个元素,连接成链表heapq是pyth
leetcode 第321题Reversed_Integer Java中的LeetCode逆整数问题解决方案 问题 - 给定一个有符号的 32 位整数 x,返回 x 其数字颠倒。 如果反转 x 导致值超出有符号的 32 位整数范围 [-231, 231 - 1],则返回 0。 ...
leetcode-cheat 的发布 它是什么 ? 这是一个chrome 扩展,可以帮助您更高效地使用 leetcode。您可以从 重要: leetcode-cheat 现在只支持中文版。 也就是说不完全支持leetcode.com,但是你可以用leetcode-cn.com代替...
~/.vscode/extensions/leetcode.vscode-leetcode-0.17.0/node_modules/vsc-leetcode-cli/bin/leetcode /usr/local/bin/leetcode 修改模板 open ~/.vscode/extensions/leetcode.vscode-leetcode-0.17.0/node_modules/...
leetcode-helper-1.7.1
leetcode-editor,在ide中做leetcode练习,支持leetcode.com和leetcode-cn.com,以满足练习的基本需求。理论上支持:intellij idea phpstorm webstorm pycharm rubymine appcode clion goland datagrip rider mps ...
leetcode-tag-dynamic programming
970. 强整数对数运算function powerfulIntegers(x: number, y: number, bound: number): numb
leetcode-tag-Tree
leetcode-cli 注意:这个存储库是为了临时使用而分叉的。 注意:从 webbrowser 复制 cookie 并使用leetcode user -c可以临时修复不能。 一个享受 leetcode 的高效 cli 工具! 非常感谢 leetcode.com,一个非常棒的...
leetcode-tag-Stack