Java 以有效的方式从已排序的链表中删除重复项
我正在使用HackerRank,我需要从已排序的链接列表中删除重复项。我通过了所有的案例,除了其中两个案例的输入类似于:10001102034 所以我的程序需要几秒钟才能完成,并且超过了时间。如何才能更有效地编写代码,我听说过使用平方根,但我不知道如何使用它。任何导游都很感激。这是我的密码Java 以有效的方式从已排序的链表中删除重复项,java,algorithm,sorting,linked-list,nodes,Java,Algorithm,Sorting,Linked List,Nodes,我正在使用HackerRank,我需要从已排序的链接列表中删除重复项。我通过了所有的案例,除了其中两个案例的输入类似于:10001102034 所以我的程序需要几秒钟才能完成,并且超过了时间。如何才能更有效地编写代码,我听说过使用平方根,但我不知道如何使用它。任何导游都很感激。这是我的密码 private static Node removeDuplicates(Node head) { /* Another reference to head */ Node c
private static Node removeDuplicates(Node head) {
/* Another reference to head */
Node current = head;
Node next;
/* Traverse list till the last node */
while (current != null && current.next != null) {
if (current.data == current.next.data) {
next = current.next.next;
if (next == null) {
current.next = null;
break;
}
current.next = next;
} else {
current = current.next;
}
}
return head;
}
再说一遍。它可以工作,但需要花费太多时间处理较长的数字。您不能使用平方根,因为当您要从列表中删除重复项时,必须检查所有列表。 平方根技术用于在排序列表中进行搜索。 但是对于你的问题,如果你能在O(n^2)中改进你的代码的运行时,但是如果你改变你的代码使用哈希表,你可以使它成为O(n) 导入java.util.HashSet 移除的公共类副本
{ 静态类节点
{ int-val; 节点下一步
public node(int val)
{
this.val = val;
}
}
/* Function to remove duplicates from a
unsorted linked list */
static void removeDuplicate(node head)
{
// Hash to store seen values
HashSet<Integer> hs = new HashSet<>();
/* Pick elements one by one */
node current = head;
node prev = null;
while (current != null)
{
int curval = current.val;
// If current value is seen before
if (hs.contains(curval)) {
prev.next = current.next;
} else {
hs.add(curval);
prev = current;
}
current = current.next;
}
}
/* Function to print nodes in a given linked list */
static void printList(node head)
{
while (head != null)
{
System.out.print(head.val + " ");
head = head.next;
}
}
公共节点(int val)
{
this.val=val;
}
}
/*函数可从文件中删除重复项
未排序链表*/
静态空洞移除副本(节点头)
{
//散列以存储所看到的值
HashSet hs=新的HashSet();
/*逐个拾取元素*/
节点电流=头;
node prev=null;
while(当前!=null)
{
int curval=current.val;
//如果之前看到当前值
如果(hs.contains(curval)){
prev.next=当前.next;
}否则{
加上(曲线);
prev=当前值;
}
当前=当前。下一步;
}
}
/*函数打印给定链表中的节点*/
静态无效打印列表(节点头)
{
while(head!=null)
{
系统输出打印(head.val+“”);
head=head.next;
}
}
我希望这会对您有所帮助。您不能使用平方根,因为当您要从列表中删除重复项时,必须检查所有列表。 平方根技术用于在排序列表中进行搜索。 但是对于你的问题,如果你能在O(n^2)中改进你的代码的运行时,但是如果你改变你的代码使用哈希表,你可以使它成为O(n) 导入java.util.HashSet 移除的公共类副本
{ 静态类节点
{ int-val; 节点下一步
public node(int val)
{
this.val = val;
}
}
/* Function to remove duplicates from a
unsorted linked list */
static void removeDuplicate(node head)
{
// Hash to store seen values
HashSet<Integer> hs = new HashSet<>();
/* Pick elements one by one */
node current = head;
node prev = null;
while (current != null)
{
int curval = current.val;
// If current value is seen before
if (hs.contains(curval)) {
prev.next = current.next;
} else {
hs.add(curval);
prev = current;
}
current = current.next;
}
}
/* Function to print nodes in a given linked list */
static void printList(node head)
{
while (head != null)
{
System.out.print(head.val + " ");
head = head.next;
}
}
公共节点(int val)
{
this.val=val;
}
}
/*函数可从文件中删除重复项
未排序链表*/
静态空洞移除副本(节点头)
{
//散列以存储所看到的值
HashSet hs=新的HashSet();
/*逐个拾取元素*/
节点电流=头;
node prev=null;
while(当前!=null)
{
int curval=current.val;
//如果之前看到当前值
如果(hs.contains(curval)){
prev.next=当前.next;
}否则{
加上(曲线);
prev=当前值;
}
当前=当前。下一步;
}
}
/*函数打印给定链表中的节点*/
静态无效打印列表(节点头)
{
while(head!=null)
{
系统输出打印(head.val+“”);
head=head.next;
}
}
我希望这将对您有所帮助。您应该将条件
if(current.data==current.next.data)
替换为,而循环并使用中断“标签”
:
out:
while (current != null && current.next != null) {
while (current.data == current.next.data) {
next = current.next.next;
if (next == null) {
current.next = null;
break out;
}
current.next = next;
}
current = current.next;
}
如果(current.data==current.next.data)
,则应将条件替换为while
循环并使用中断“标签”
:
out:
while (current != null && current.next != null) {
while (current.data == current.next.data) {
next = current.next.next;
if (next == null) {
current.next = null;
break out;
}
current.next = next;
}
current = current.next;
}
输入列表已排序,无需使用HashMap:)输入列表已排序,无需使用HashMap:)我们如何在Java中“释放”它们?是的,如果我们彻底“释放”了不需要的节点,垃圾收集器决定帮助我们释放内存,它可能会阻止整个世界:)我们如何在Java中“释放”它们?是的,如果我们“释放”了,这里不需要的节点太彻底,垃圾收集器决定帮助我们释放内存,它可能会停止世界:)