【发布时间】:2021-11-20 06:05:41
【问题描述】:
我正在做一个从 LinkedList 中删除重复项的问题。首先在 if 语句中编写了此代码我已经编写了 if(curr.data == curr.next.data) 但它显示错误,并且在一个测试用例中显示运行时错误。两者不是一样吗?使用(curr.data.equals(curr.next.data))有什么区别?
public static LinkedListNode<Integer> removeDuplicates(LinkedListNode<Integer> head) {
//Your code goes here
if (head == null)
return null;
LinkedListNode<Integer> curr = head;
while(curr.next != null)
{
if(curr.data == curr.next.data)
{
curr.next = curr.next.next;
}
else
{
curr = curr.next;
}
}
return head;
}
【问题讨论】:
-
==和equals()之间的区别:stackoverflow.com/questions/7520432/…。你知道那个测试用例的输入是什么吗? -
@LinuxGeek 不,测试用例被锁定
-
@anarchist912 那是 Java
-
我认为你应该考虑@LinuxGeek 的回答。
标签: linked-list singly-linked-list dsa