由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 弱问题,连反转链表都看不懂了
相关主题
Reverse LinkedList II 怎样一遍写对啊?透露两个G的onsite题
一道挺简单的题给搞砸了面试题
问个reverse linked list到底怎么了?很多面试来的居然连reverse一个链表都写不来
问了一个链表,1->2->3->4->5, 每两个交换,2->1->4->3->5,请问大牛们如何提高解决leetcode上面Linkedlist的题的能力?
反转链表有几种方法leetcode上这个链表节点的定义是什么意思?ListNode(int x) : val(x), next(NULL) {}
合并两个排序好的链表, 优解?f 的面经
弱问一个小问题,leetcode 上merge sorted list链表插入排序都写了一个小时,对人生失去信心了。
问一个merge K sorted list的时间复杂度java 链表里面dummy node 一问?谢谢
相关话题的讨论汇总
话题: cur话题: listnode话题: newhead话题: null话题: head
进入JobHunting版参与讨论
1 (共1页)
A******g
发帖数: 612
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:
Given 1->2->3->4->5->NULL, m = 2 and n = 4,
return 1->4->3->2->5->NULL.
Google了一个解法,https://gist.github.com/eclipse9614/4757816
通过测试了
根据这个算法,模拟一下
如图,好像不是很对,肯定是我理解有问题
搞了一晚上了,太挫了!
p*g
发帖数: 141
2
pass OJ
public ListNode reverseBetween(ListNode head, int m, int n) {
ListNode preHead = new ListNode(1), cur=head;
preHead.next = head;

for ( int i=0; i cur = cur.next;
preHead = preHead.next;
}
ListNode tail = cur;
for ( int i=m; i<=n; i++) {
ListNode t = cur.next;
cur.next = preHead.next;
preHead.next = cur;
cur = t;
}
tail.next = cur;
return m==1? preHead.next:head;
}

【在 A******g 的大作中提到】
: 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->2->3->4->5->NULL, m = 2 and n = 4,
: return 1->4->3->2->5->NULL.
: Google了一个解法,https://gist.github.com/eclipse9614/4757816
: 通过测试了
: 根据这个算法,模拟一下
: 如图,好像不是很对,肯定是我理解有问题
: 搞了一晚上了,太挫了!

A******g
发帖数: 612
3
能解释一下算法吗,谢谢!
p*****p
发帖数: 379
4
这人写的略乱……
按他的意思
mHead是m点那个
nNode是n后面那个
tmphead造出来不知道干嘛用的
这题用递归容易看

【在 A******g 的大作中提到】
: 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->2->3->4->5->NULL, m = 2 and n = 4,
: return 1->4->3->2->5->NULL.
: Google了一个解法,https://gist.github.com/eclipse9614/4757816
: 通过测试了
: 根据这个算法,模拟一下
: 如图,好像不是很对,肯定是我理解有问题
: 搞了一晚上了,太挫了!

c********t
发帖数: 5706
5
赞图
这道题很难写对,
先试试把 reverse whole list 写对。
然后m to n 就是如何把头尾与原来的连接好了
还有就是多用变量。

【在 A******g 的大作中提到】
: 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->2->3->4->5->NULL, m = 2 and n = 4,
: return 1->4->3->2->5->NULL.
: Google了一个解法,https://gist.github.com/eclipse9614/4757816
: 通过测试了
: 根据这个算法,模拟一下
: 如图,好像不是很对,肯定是我理解有问题
: 搞了一晚上了,太挫了!

A******g
发帖数: 612
6
谢谢大家指点! 用recursion果然很快写完了,而且比较容易看懂
r*********n
发帖数: 4553
7
i know this is cheating, but can't you just swap the values of the two nodes
?
l*******b
发帖数: 2586
8
反转整个链表貌似好写点, 反转 m, n需要有几个边角情况, leetcode上的解法挺好呀.
val c = List(1,2,3,4,5)
def rev(x:List[Int], t:List[Int]) : List[Int] = {
if (x.tail == Nil) return x ::: t
else rev(x.tail, x.head :: t)
}
rev(c, Nil)
得到
res1: List[Int] = List(5, 4, 3, 2, 1)
i********m
发帖数: 332
9
练习了
public ListNode reverseBetween(ListNode head, int m, int n) {
// Start typing your Java solution below
// DO NOT write main() function

int diff = n-m;
if (diff < 0 || m <= 0)
return null;
boolean flag = false;
ListNode cur = head;
ListNode prev = head;

while (m-- > 1) {
if (cur == head) {
cur = cur.next;
}
else {
cur = cur.next;
prev = prev.next;
}

if (cur == null) {
return null;
}
}

ListNode newHead = cur;
ListNode newPrev = newHead;
if (newHead == head)
flag = true;


while (diff-- > 0 ){
if (cur == null)
return null;
if (cur == newHead) {
cur = cur.next;
if (cur == null)
return null;
}
newPrev.next = cur.next;
cur.next = newHead;
newHead = cur;
cur = newPrev.next;
}


if (flag == false){
prev.next = newHead;
return head;
}
else
return newHead;
}
A*****i
发帖数: 3587
10
这做法我觉得很好
很多情况下list里面的data都存的不过是个索引而已,C/C++下面存对象(数据)的地址
,java下存的是reference,就算真存的是int或者单一的数据也很简单,根本没有必要
来操作那个next指针
但是面试官能不能过就另一回事

nodes

【在 r*********n 的大作中提到】
: i know this is cheating, but can't you just swap the values of the two nodes
: ?

A******g
发帖数: 612
11
链表没有random access,swap value的话要jump back and forth, 复杂度大概是O(N
^2)。 recursion的话等于把每个节点的地址都存到stack里,所以时间是O(N),空间也
是O(N)。 话说回来,如果先用一个array把每个链表节点的地址存起来,那么既可以反
转next,也可以swap value, 都是O(N),和recursion一样了,code也挺好写。
谢谢你给的灵感! 对这题的认识又加深了。

nodes

【在 r*********n 的大作中提到】
: i know this is cheating, but can't you just swap the values of the two nodes
: ?

c********t
发帖数: 5706
12
Think again

(N
★ 发自iPhone App: ChineseWeb 7.8

【在 A******g 的大作中提到】
: 链表没有random access,swap value的话要jump back and forth, 复杂度大概是O(N
: ^2)。 recursion的话等于把每个节点的地址都存到stack里,所以时间是O(N),空间也
: 是O(N)。 话说回来,如果先用一个array把每个链表节点的地址存起来,那么既可以反
: 转next,也可以swap value, 都是O(N),和recursion一样了,code也挺好写。
: 谢谢你给的灵感! 对这题的认识又加深了。
:
: nodes

1 (共1页)
进入JobHunting版参与讨论
相关主题
java 链表里面dummy node 一问?谢谢反转链表有几种方法
两个链表怎么查找相交点?合并两个排序好的链表, 优解?
问了一个链表,1->2->3->4->5, 每两个交换,2->1->4->3->5弱问一个小问题,leetcode 上merge sorted list
leetcode过的一代工程师问一个merge K sorted list的时间复杂度
Reverse LinkedList II 怎样一遍写对啊?透露两个G的onsite题
一道挺简单的题给搞砸了面试题
问个reverse linked list到底怎么了?很多面试来的居然连reverse一个链表都写不来
问了一个链表,1->2->3->4->5, 每两个交换,2->1->4->3->5,请问大牛们如何提高解决leetcode上面Linkedlist的题的能力?
相关话题的讨论汇总
话题: cur话题: listnode话题: newhead话题: null话题: head