Redian新闻
>
如何删除 linked list 的最后一个元素
avatar
如何删除 linked list 的最后一个元素# Java - 爪哇娇娃
t*y
1
大家好,请问如果在Joann网上看布料,如何分辨哪种厚度是坐窗帘,家具布艺,哪种
是做衣服的呢?
谢谢!
avatar
p*s
2
谢谢
avatar
l*i
3
parameter 只有一个 Node,要被删除。
private static boolean removeNode(Node toRemoved)
{
if(toRemoved == null)
{
return false;
}
if(toRemoved.next == null) //This is not working.
{
toRemoved = null;
return true;
}
Node next = toRemoved.next;
toRemoved.data = next.data;
toRemoved.next = next.next;
return true;
}
我想这个node如果是最后一个元素的话,设其为 null
但是实际跑的时候,发现这个node并没有删除。请问为什么?有什么办法直接删除
linked list 的最后一个元素 ?
多谢。
avatar
c*r
4
做窗帘,家具之类的称为decorator fabric。做衣服的料很多了,比如说cotton woven
(棉布), cotton knit (棉的有收缩性的,类似国内做内衣的料),twill 或者
jeans, silk。我建议你去店里看看,感觉一下就知道了。
avatar
h*v
5
看你最近挺忙的。一边帮人看房子,有个大门facing north的,但是,买家couldn't
afford it。。。 一边还要跟有tax evasion的evil guy打交道。。。
Just kidding.
avatar
B*g
6
removeLast()?

【在 l***i 的大作中提到】
: parameter 只有一个 Node,要被删除。
: private static boolean removeNode(Node toRemoved)
: {
: if(toRemoved == null)
: {
: return false;
: }
: if(toRemoved.next == null) //This is not working.
: {
: toRemoved = null;

avatar
wh
7
哈哈,你看帖仔细。best luck to 楼主,呵呵。你要搬家吗?看你的签名档。

【在 h******v 的大作中提到】
: 看你最近挺忙的。一边帮人看房子,有个大门facing north的,但是,买家couldn't
: afford it。。。 一边还要跟有tax evasion的evil guy打交道。。。
: Just kidding.

avatar
g*g
8
来问作业?
if(current.next.next == null) {
current.next = null;
} else {
current = current.next;
}
加上检查小于等于1个结点的特殊情况就可以了。

【在 l***i 的大作中提到】
: parameter 只有一个 Node,要被删除。
: private static boolean removeNode(Node toRemoved)
: {
: if(toRemoved == null)
: {
: return false;
: }
: if(toRemoved.next == null) //This is not working.
: {
: toRemoved = null;

avatar
c*e
9
这么麻烦? 和c++一样了。

【在 g*****g 的大作中提到】
: 来问作业?
: if(current.next.next == null) {
: current.next = null;
: } else {
: current = current.next;
: }
: 加上检查小于等于1个结点的特殊情况就可以了。

avatar
J*n
10
toRemove.next==null的情况应该直接return false,因为你只给toRemove这个节点,
而节点又只有指向next的reference,没有previous,那如果toRemove是最后一个节点
,完全无解啊

【在 l***i 的大作中提到】
: parameter 只有一个 Node,要被删除。
: private static boolean removeNode(Node toRemoved)
: {
: if(toRemoved == null)
: {
: return false;
: }
: if(toRemoved.next == null) //This is not working.
: {
: toRemoved = null;

avatar
c*e
11
while (current.next.next != null) {
current = current.next;
}
current.next = null;

【在 g*****g 的大作中提到】
: 来问作业?
: if(current.next.next == null) {
: current.next = null;
: } else {
: current = current.next;
: }
: 加上检查小于等于1个结点的特殊情况就可以了。

avatar
l*i
12
题目的要求是从一个linkedlist里把某个node删除。
我看不出你的code如何把这个node删除了,你只是把当前的pointer移到了下一个node。

【在 g*****g 的大作中提到】
: 来问作业?
: if(current.next.next == null) {
: current.next = null;
: } else {
: current = current.next;
: }
: 加上检查小于等于1个结点的特殊情况就可以了。

avatar
l*i
13
请注意,这个问题要求parameter只有一个,就是当前的node的pointer,没有整个list
的信息。而且这个要求自己implement,不能用java现有的library。
我的问题是,既然倒数第二个的next指向最后一个,那我把最后一个设为null,不就相
当与删除了这个node吗?是不是有什么java底层的东西我搞错了?

【在 B*****g 的大作中提到】
: removeLast()?
avatar
c*e
14
有娃大妈真是认真啊。学过数据结构吗?
你打算怎么把最后一个设为null呢?

list

【在 l***i 的大作中提到】
: 请注意,这个问题要求parameter只有一个,就是当前的node的pointer,没有整个list
: 的信息。而且这个要求自己implement,不能用java现有的library。
: 我的问题是,既然倒数第二个的next指向最后一个,那我把最后一个设为null,不就相
: 当与删除了这个node吗?是不是有什么java底层的东西我搞错了?

avatar
J*n
15
toRemove只是一个reference,相当于一个指针,而指向这个要删的Node object的还有
其他指针(比如前一个节点的next),你把toRemove=null以后toRemove这个指针是不
指向你要删除的节点了,但是别忘了该节点的前一个节点的next reference还是指向该
节点的。这跟Java没关系。这样当从LinkedList的第一个node遍历的时候,还是可以走
到要删的节点,说的有点绕,希望意思表达明白了。。。

list

【在 l***i 的大作中提到】
: 请注意,这个问题要求parameter只有一个,就是当前的node的pointer,没有整个list
: 的信息。而且这个要求自己implement,不能用java现有的library。
: 我的问题是,既然倒数第二个的next指向最后一个,那我把最后一个设为null,不就相
: 当与删除了这个node吗?是不是有什么java底层的东西我搞错了?

avatar
J*n
16
貌似知道你为啥进入死胡同了。。。从linkedlist里删掉一个node的意思就是这个node
前一个node的next pointer不指向它,这个node后一个node的previous pointer也不指
向它,就表示删掉了,因为这个node就跟linkedlist没关系了。不是说把这个node
object清掉才叫删掉。。。

node。

【在 l***i 的大作中提到】
: 题目的要求是从一个linkedlist里把某个node删除。
: 我看不出你的code如何把这个node删除了,你只是把当前的pointer移到了下一个node。

avatar
c*e
17
如果node是new造出来的,最好能delete 掉,释放空间。当然,如果是java,就不用管
了。

node

【在 J*******n 的大作中提到】
: 貌似知道你为啥进入死胡同了。。。从linkedlist里删掉一个node的意思就是这个node
: 前一个node的next pointer不指向它,这个node后一个node的previous pointer也不指
: 向它,就表示删掉了,因为这个node就跟linkedlist没关系了。不是说把这个node
: object清掉才叫删掉。。。
:
: node。

avatar
a*e
18

得先判断 current.next 是否 null

【在 c*********e 的大作中提到】
: 如果node是new造出来的,最好能delete 掉,释放空间。当然,如果是java,就不用管
: 了。
:
: node

avatar
c*e
19
linked list最后一个node才指向null,循环里面,current最远只指向倒数第二个node

【在 a**e 的大作中提到】
:
: 得先判断 current.next 是否 null

avatar
c*e
20
恩,这个属于特例,只有一个node,要在loop前先特别对待。

【在 a**e 的大作中提到】
:
: 得先判断 current.next 是否 null

avatar
l*i
21
呵呵,你解释得很明白。谢谢。
在C++里可以用pointer的reference直接改变一个memory location的值,比如下面这个
#include
using namespace std;
void update(int*);
int main()
{
int x = 5;
int* pi = &x;
cout << "Before update, " << x << endl;

update(pi);
cout << "After update, " << x << endl;
return 0;
}
void update(int* t)
{
*t = 8;
}
输出是
Before update, 5
After update, 8
是不是可以说,在java里 没有相应的功能?

node

【在 J*******n 的大作中提到】
: 貌似知道你为啥进入死胡同了。。。从linkedlist里删掉一个node的意思就是这个node
: 前一个node的next pointer不指向它,这个node后一个node的previous pointer也不指
: 向它,就表示删掉了,因为这个node就跟linkedlist没关系了。不是说把这个node
: object清掉才叫删掉。。。
:
: node。

avatar
c*e
22
java里面这个方面比较tricky。

这个

【在 l***i 的大作中提到】
: 呵呵,你解释得很明白。谢谢。
: 在C++里可以用pointer的reference直接改变一个memory location的值,比如下面这个
: #include
: using namespace std;
: void update(int*);
: int main()
: {
: int x = 5;
: int* pi = &x;
: cout << "Before update, " << x << endl;

avatar
J*n
23
In Java, primitives are passed by value, objects are passed by reference.
in Java, if you have
int x = 5;
int y = x;
then you update y, x won't be updated, because int is primitive, so y has
its own value instead of an pointer to value of x.
if you have
Object x = new Object();
Object y = x;
then you update y, the object that x points to will be updated as well,
because reference x is passed to reference y.

这个

【在 l***i 的大作中提到】
: 呵呵,你解释得很明白。谢谢。
: 在C++里可以用pointer的reference直接改变一个memory location的值,比如下面这个
: #include
: using namespace std;
: void update(int*);
: int main()
: {
: int x = 5;
: int* pi = &x;
: cout << "Before update, " << x << endl;

avatar
c*e
24
in java,it's all passed by value. e.g.
main() {
Person p= new Person("Jack");
foo(p);
system.out.println(p);
//you need to write an override tostring() in Person class
}
================
public void foo(Person s) {
s=new Person("Tom");
}

【在 J*******n 的大作中提到】
: In Java, primitives are passed by value, objects are passed by reference.
: in Java, if you have
: int x = 5;
: int y = x;
: then you update y, x won't be updated, because int is primitive, so y has
: its own value instead of an pointer to value of x.
: if you have
: Object x = new Object();
: Object y = x;
: then you update y, the object that x points to will be updated as well,

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。