刪除重複結點(鍊錶)

2021-09-27 23:41:33 字數 1135 閱讀 2952

在乙個排序的鍊錶中,存在重複的結點,請刪除該鍊錶中重複的結點,重複的結點不保留,返回煉表頭指標。 例如,鍊錶1->2->3->3->4->4->5 處理後為 1->2->5

題目**:

tpid=13&&tqid=11209&rp=1&ru=/activity/oj&qru=/ta/coding-interviews/question-ranking

這道題的思路不難,我們將鍊錶中不重複出現的數字放入新煉表中,首先我們要遍歷整個鍊錶,找到第一組出現重複數字的結點,然後進行第二次遍歷,遍歷這組重複數字的結點,並且將他們跳過。

具體實現**如下

/*

public class listnode }*/

public

class

solution

listnode newhead =

newlistnode(-

1); listnode newtail = newhead;

listnode cur = phead;

while

(cur != null)

cur = cur.next;

}else

}return newhead.next;

}}

方法二:hwc老鐵

/*

public class listnode }*/

public

class

solution

listnode newhead =

newlistnode(-

1); newhead.next = phead;

listnode prev = newhead;

listnode node = prev.next;

while

(node != null)

prev.next = node.next;

node = node.next;

}else

}return newhead.next;

}}

刪除鍊錶中重複的結點

題目描述 在乙個排序的鍊錶中,存在重複的結點,請刪除該鍊錶中重複的結點,重複的結點不保留,返回煉表頭指標。例如,鍊錶1 2 3 3 4 4 5 處理後為 1 2 5。刪除鍊錶中重複的結點 author 過路的守望 public class duplicationnode 新建乙個節點指向頭結點 li...

刪除鍊錶中重複的結點

在乙個排序的鍊錶中,存在重複的結點,請刪除該鍊錶中重複的結點,重複的結點不保留,返回煉表頭指標。例如,鍊錶1 2 3 3 4 4 5 處理後為 1 2 5 解法一 遞迴 public class listnode public class solution if phead.next.val phe...

刪除鍊錶中重複的結點

在乙個排序的鍊錶中,存在重複的結點,請刪除該鍊錶中重複的結點,重複的結點不保留,返回煉表頭指標。例如,鍊錶1 2 3 3 4 4 5 處理後為 1 2 5 題目分析 刪除鍊錶中的結點要比較當前結點是否與前面結點和後面結點相同,只有兩個都不同的結點才保留。用pre儲存前乙個節點,cur儲存當前結點,c...