【在 c***2 的大作中提到】 : Remove a node from a singly linkedlist without knowing the head node. All : you have is the node itself.
d*e
4 楼
re这个,我觉得不行
【在 h***o 的大作中提到】 : please understand what is "a" node : this is tricky
c*2
5 楼
1) Try to run it, got infinite loop with random addresses 2) This is a wrong question
c*2
6 楼
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
7 楼
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
8 楼
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
9 楼
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
10 楼
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
11 楼
sentinel
【在 c***2 的大作中提到】 : Remove a node from a singly linkedlist without knowing the head node. All : you have is the node itself.
c*2
12 楼
The question shall be modified as "delete a *middle* node".
l*a
13 楼
Don't u think that is a hint?
【在 c***2 的大作中提到】 : The question shall be modified as "delete a *middle* node".
c*2
14 楼
otherwise, there is no solution for the last node. AN dturns this question invalid.
l*v
15 楼
sentinel node
【在 c***2 的大作中提到】 : otherwise, there is no solution for the last node. AN dturns this question : invalid.
c*2
16 楼
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
17 楼
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
18 楼
Finally got it: with a fixed dummy tail node, there is NO last node. Thanks,