由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 问到linked list 的题目
相关主题
LRU C++过不了delete a node in linked list
reverse random pointers of a single linked list问个问题post order traveral using interation
google面试全过程(简装版)问个二叉树删除结点的问题
careercup第12.5题目求助?弱问怎么判断两个binary tree相同?
发现一个很恶心的基础问题"简单的"linklist的问题
感觉今天结结实实被烙印阴了remove a node in O(1) from link list
leetcode 一道简单题的疑问linklist exercise
回馈本版,新鲜店面,新题新气象分享:non-recursive breadth first search and depth first search algorithm in C
相关话题的讨论汇总
话题: ptr话题: node话题: null话题: next话题: previous
进入JobHunting版参与讨论
1 (共1页)
s*****r
发帖数: 773
1
given the access to a node, how to delete it.
这个题目有个特殊情况是如果这个node是尾结点, 请问在不知道head的情况下怎么处理?
r****o
发帖数: 1950
2
delete(**p)
{
...
if (*p)&&(*p->next==NULL)
*p=NULL;
...
}
行吗?

理?

【在 s*****r 的大作中提到】
: given the access to a node, how to delete it.
: 这个题目有个特殊情况是如果这个node是尾结点, 请问在不知道head的情况下怎么处理?

y**i
发帖数: 1112
3
如果不是尾结点该怎么delete?假设是单向链表

理?

【在 s*****r 的大作中提到】
: given the access to a node, how to delete it.
: 这个题目有个特殊情况是如果这个node是尾结点, 请问在不知道head的情况下怎么处理?

l*****a
发帖数: 14598
4
既没有head
有没有previous
你觉得能处理吗?

理?

【在 s*****r 的大作中提到】
: given the access to a node, how to delete it.
: 这个题目有个特殊情况是如果这个node是尾结点, 请问在不知道head的情况下怎么处理?

n***r
发帖数: 105
5
很close了。我觉得这题是考pointer's pointer。
但我觉得怎么招都得知道前一个node的地址,得把其next pointer赋值为NULL,否则就
断链了。用pointer's pointer的好处是在遍历这个linked list的时候可以省去一个指向前一
个node的pointer。
比如说删除存有item为i的程序就可以这样写,但是还是require work through这个
list,以便持有前一个node的地址。
struct list **lpp;
for(lpp = &list; *lpp != NULL; lpp = &(*lpp)->next)
{
if((*lpp)->item == i)
{
*lpp = (*lpp)->next;
break;
}
}
}

用这段程序,就算删除的对象是尾node,也自动take care了。欢迎讨论

【在 r****o 的大作中提到】
: delete(**p)
: {
: ...
: if (*p)&&(*p->next==NULL)
: *p=NULL;
: ...
: }
: 行吗?
:
: 理?

h**6
发帖数: 4160
6
显然不行。你把尾指针赋值为NULL了,可是前一个结点的next还是没有变。

【在 r****o 的大作中提到】
: delete(**p)
: {
: ...
: if (*p)&&(*p->next==NULL)
: *p=NULL;
: ...
: }
: 行吗?
:
: 理?

d**e
发帖数: 6098
7
but prev_node->next == p
after setting p = NULL, prev_node is the last node

【在 h**6 的大作中提到】
: 显然不行。你把尾指针赋值为NULL了,可是前一个结点的next还是没有变。
s******a
发帖数: 103
8
void DeleteNode(Node *ptr)
{
if (ptr==null || ptr->next==null)
return; //Doesn't work for tail node
Node *temp=ptr->next;
ptr->value=temp->value;
ptr->next=temp->next;
delete temp;
}

理?

【在 s*****r 的大作中提到】
: given the access to a node, how to delete it.
: 这个题目有个特殊情况是如果这个node是尾结点, 请问在不知道head的情况下怎么处理?

r****o
发帖数: 1950
9
为什么都说this way does not work for tail node呢?
是不是我哪儿理解的不对?我的想法是
void DeleteNode(Node **ptr)
{
if (*ptr==null)
return; //Doesn't work for tail node
if (*ptr->next==NULL) //tail node
{
free(*ptr);
*ptr=NULL;
}else{
Node *temp=*ptr->next;
*ptr->value=temp->value;
*ptr->next=temp->next;
free(temp);
}
}

【在 s******a 的大作中提到】
: void DeleteNode(Node *ptr)
: {
: if (ptr==null || ptr->next==null)
: return; //Doesn't work for tail node
: Node *temp=ptr->next;
: ptr->value=temp->value;
: ptr->next=temp->next;
: delete temp;
: }
:

r****y
发帖数: 4494
10
tail node的情况,把ptr本身设成NULL没用,前一个node的next的值还是ptr原来的值。

【在 r****o 的大作中提到】
: 为什么都说this way does not work for tail node呢?
: 是不是我哪儿理解的不对?我的想法是
: void DeleteNode(Node **ptr)
: {
: if (*ptr==null)
: return; //Doesn't work for tail node
: if (*ptr->next==NULL) //tail node
: {
: free(*ptr);
: *ptr=NULL;

相关主题
感觉今天结结实实被烙印阴了delete a node in linked list
leetcode 一道简单题的疑问问个问题post order traveral using interation
回馈本版,新鲜店面,新题新气象问个二叉树删除结点的问题
进入JobHunting版参与讨论
s******a
发帖数: 103
11
The previous node is pointing at the same node as your ptr. So if you do
free(ptr), then the previous node will become a dangling pointer.

【在 r****o 的大作中提到】
: 为什么都说this way does not work for tail node呢?
: 是不是我哪儿理解的不对?我的想法是
: void DeleteNode(Node **ptr)
: {
: if (*ptr==null)
: return; //Doesn't work for tail node
: if (*ptr->next==NULL) //tail node
: {
: free(*ptr);
: *ptr=NULL;

r****o
发帖数: 1950
12
free(ptr)只是free ptr 指向的那个空间,ptr的值没变啊。

【在 s******a 的大作中提到】
: The previous node is pointing at the same node as your ptr. So if you do
: free(ptr), then the previous node will become a dangling pointer.

s******a
发帖数: 103
13
If you want to delete the node pointed by ptr which is the tail node in your
case, then what you need to do is delete the node from the memory, and set
previous->next to be null. But there is no way you can access the previous->
next if you are only given ptr in the singly linked list. Even you delete
the object pointed by ptr, the previous->next still points to the same
location in memory even though the reference has since been deleted and may
now be used for other purposes. So the previous->

【在 r****o 的大作中提到】
: free(ptr)只是free ptr 指向的那个空间,ptr的值没变啊。
r****o
发帖数: 1950
14
Thanks, previous->next=ptr;
我free了ptr,并且将ptr置为NULL,为啥previous->next还会是个dangling pointer呢?
你说的may now be used for other purposes是指什么呢?

your
set
->
may
dangling

【在 s******a 的大作中提到】
: If you want to delete the node pointed by ptr which is the tail node in your
: case, then what you need to do is delete the node from the memory, and set
: previous->next to be null. But there is no way you can access the previous->
: next if you are only given ptr in the singly linked list. Even you delete
: the object pointed by ptr, the previous->next still points to the same
: location in memory even though the reference has since been deleted and may
: now be used for other purposes. So the previous->

d**e
发帖数: 6098
15
i see.... thanks

your
set
->
may
dangling

【在 s******a 的大作中提到】
: If you want to delete the node pointed by ptr which is the tail node in your
: case, then what you need to do is delete the node from the memory, and set
: previous->next to be null. But there is no way you can access the previous->
: next if you are only given ptr in the singly linked list. Even you delete
: the object pointed by ptr, the previous->next still points to the same
: location in memory even though the reference has since been deleted and may
: now be used for other purposes. So the previous->

s******a
发帖数: 103
16
First of all, you can NOT write "previous->next=ptr" in your code, because
you will not be able to access "previous". And "previous->next" and ptr are
two different pointers. Setting ptr to be null doesn't affect "previous->
next".

呢?

【在 r****o 的大作中提到】
: Thanks, previous->next=ptr;
: 我free了ptr,并且将ptr置为NULL,为啥previous->next还会是个dangling pointer呢?
: 你说的may now be used for other purposes是指什么呢?
:
: your
: set
: ->
: may
: dangling

r****o
发帖数: 1950
17
我明白你的意思了,多谢。

your
set
->
may
dangling

【在 s******a 的大作中提到】
: If you want to delete the node pointed by ptr which is the tail node in your
: case, then what you need to do is delete the node from the memory, and set
: previous->next to be null. But there is no way you can access the previous->
: next if you are only given ptr in the singly linked list. Even you delete
: the object pointed by ptr, the previous->next still points to the same
: location in memory even though the reference has since been deleted and may
: now be used for other purposes. So the previous->

y**i
发帖数: 1112
18
原来是把下一个结点的值拷贝到当前节点,然后删除下一个结点啊,感觉是偷换概念。
不过不这样在不知道头的情况下应该就没有其他办法了

【在 s******a 的大作中提到】
: void DeleteNode(Node *ptr)
: {
: if (ptr==null || ptr->next==null)
: return; //Doesn't work for tail node
: Node *temp=ptr->next;
: ptr->value=temp->value;
: ptr->next=temp->next;
: delete temp;
: }
:

y**i
发帖数: 1112
19
看来要删除尾结点,就只有传递结点指针的地址或引用了?比如
void DeleteNode(Node **pptr)

【在 s******a 的大作中提到】
: void DeleteNode(Node *ptr)
: {
: if (ptr==null || ptr->next==null)
: return; //Doesn't work for tail node
: Node *temp=ptr->next;
: ptr->value=temp->value;
: ptr->next=temp->next;
: delete temp;
: }
:

r****o
发帖数: 1950
20
这个方法不适用于尾节点,
见sonicsea的贴.

【在 y**i 的大作中提到】
: 看来要删除尾结点,就只有传递结点指针的地址或引用了?比如
: void DeleteNode(Node **pptr)

1 (共1页)
进入JobHunting版参与讨论
相关主题
分享:non-recursive breadth first search and depth first search algorithm in C发现一个很恶心的基础问题
Tricky Pointer Problems -- Which level are you?感觉今天结结实实被烙印阴了
sorted linked list里insert一个nodeleetcode 一道简单题的疑问
remove a node (and its memory) from a doubly linked list回馈本版,新鲜店面,新题新气象
LRU C++过不了delete a node in linked list
reverse random pointers of a single linked list问个问题post order traveral using interation
google面试全过程(简装版)问个二叉树删除结点的问题
careercup第12.5题目求助?弱问怎么判断两个binary tree相同?
相关话题的讨论汇总
话题: ptr话题: node话题: null话题: next话题: previous