boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - remove a node in O(1) from link list
相关主题
发个pure storage的interviewstreet题目
菜鸟 贴一个 leetcode LRU Cache -- java代码,并求解疑惑。
刚面完的2道题,我做的稀烂
再问大牛们leetcode上面Linkedlist的题,Reverse Nodes in k-G
array 转换成 linkedlist, 在线等, 挺急的--help is still nee
一个Linkedlist面试题的教训
明天电面,求建议
remove a node (and its memory) from a doubly linked list
如何删除 linked list 的最后一个元素 (转载)
回馈本版,新鲜店面,新题新气象
相关话题的讨论汇总
话题: node话题: remove话题: null话题: tail话题: list
进入JobHunting版参与讨论
1 (共1页)
c***2
发帖数: 838
1
Remove a node from a singly linkedlist without knowing the head node. All
you have is the node itself.
h***o
发帖数: 2321
2
please understand what is "a" node
this is tricky

【在 c***2 的大作中提到】
: Remove a node from a singly linkedlist without knowing the head node. All
: you have is the node itself.

d**e
发帖数: 6098
3
re这个,我觉得不行

【在 h***o 的大作中提到】
: please understand what is "a" node
: this is tricky

c***2
发帖数: 838
4
1) Try to run it, got infinite loop with random addresses
2) This is a wrong question
c***2
发帖数: 838
5
This is not in an interview, if you know the trick, spit it out.

【在 h***o 的大作中提到】
: please understand what is "a" node
: this is tricky

d**e
发帖数: 6098
6
because that node is not really deleted.
you made a trick to simply modify the value inside that node and delete its
next node. from the outside, if you retrieve the value information, it looks
like the node was deleted. But actually, it is not.
if we have such case:
c***2
发帖数: 838
7
Yes, that's definitely right.
But over here, we don't have access to p;
How to set it to NULL?
Suppose
p->next=q
free(q);
q=NULL;
this won't set p->next to NULL.
I am really confused.
I only use C, can any other lanuague support this feature?

its
looks

【在 d**e 的大作中提到】
: because that node is not really deleted.
: you made a trick to simply modify the value inside that node and delete its
: next node. from the outside, if you retrieve the value information, it looks
: like the node was deleted. But actually, it is not.
: if we have such case:

c***n
发帖数: 809
8
remember the previous one,
set the previous->next = cur-> next,
delete cur

【在 c***2 的大作中提到】
: Remove a node from a singly linkedlist without knowing the head node. All
: you have is the node itself.

l*****a
发帖数: 14598
9
if it is the last node of the linked list
you can only start from the beginning.
u need to know the head.otherwise u can't solve this problem.
for time complexity,
(n-1 * 1 +1 *n-1)/n=O(1)

【在 c***2 的大作中提到】
: Yes, that's definitely right.
: But over here, we don't have access to p;
: How to set it to NULL?
: Suppose
: p->next=q
: free(q);
: q=NULL;
: this won't set p->next to NULL.
: I am really confused.
: I only use C, can any other lanuague support this feature?

l*****v
发帖数: 498
10
sentinel

【在 c***2 的大作中提到】
: Remove a node from a singly linkedlist without knowing the head node. All
: you have is the node itself.

相关主题
再问大牛们leetcode上面Linkedlist的题,Reverse Nodes in k-G
array 转换成 linkedlist, 在线等, 挺急的--help is still nee
一个Linkedlist面试题的教训
明天电面,求建议
进入JobHunting版参与讨论
c***2
发帖数: 838
11
The question shall be modified as "delete a *middle* node".
l*****a
发帖数: 14598
12
Don't u think that is a hint?

【在 c***2 的大作中提到】
: The question shall be modified as "delete a *middle* node".
c***2
发帖数: 838
13
otherwise, there is no solution for the last node. AN dturns this question
invalid.
l*****v
发帖数: 498
14
sentinel node

【在 c***2 的大作中提到】
: otherwise, there is no solution for the last node. AN dturns this question
: invalid.

c***2
发帖数: 838
15
A dummy tail node.
that works.
free(node);
node=tail;
when traverse: while(p) ==> while(p!=tail).
But this does not comply with the question: "All
you have is the node itself." :-)
l*****v
发帖数: 498
16
with senitel tail node, you don't have special case for last node. Here is
modification of your original code
boolean removeNode(Node * p){
Node * temp;
if (NULL == p){
return FALSE;
}
temp = p.next;
p.value = temp.value;
p.next = temp.next;
free(temp);
return TRUE;
}
c***2
发帖数: 838
17
Finally got it: with a fixed dummy tail node, there is NO last node.
Thanks,
1 (共1页)
进入JobHunting版参与讨论
相关主题
回馈本版,新鲜店面,新题新气象
热腾腾的 LinkedIn 电面题攒RP
我恨iPhone@Facebook电面
linklist exercise
【我自己写的LinkedList为什么总有错?】
问了一个链表,1->2->3->4->5, 每两个交换,2->1->4->3->5
amazon onsite 面经
问了一个链表,1->2->3->4->5, 每两个交换,2->1->4->3->5,
判断linkedlist是否palindrome最优解法是什么?
求个java版本的binary tree serialization和deserialization
相关话题的讨论汇总
话题: node话题: remove话题: null话题: tail话题: list