解法一:若是不得使用臨時緩衝區,該怎麼解決?java
要想移除鏈表中的重複節點,咱們須要設法記錄有哪些是重複的。這裏只須要使用到一個簡單的散列表。node
在下面的解法中,咱們會直接迭代訪問整個鏈表,將每一個節點加入到散列表。若發現有重複的元素,將該節點從鏈表中移除,而後繼續迭代。這個題目使用了鏈表,所以只須要掃描一次就能搞定。app
deleteDups()的時間複雜度爲O(N),其中N爲鏈表節點數目。this
解法二:不使用緩衝區指針
如不借助額外的緩衝區,能夠用兩個指針來迭代:current迭代訪問整個鏈表,runner用於檢查後續的節點是否重複code
上述代碼中的deleteDups2()get
該代碼的空間複雜度爲O(1),時間複雜度爲O(N2)io
package cglib;
import java.util.Hashtable; table
class LinkedListNode<T> {
public LinkedListNode<T> next;
public T data;class
public LinkedListNode(Object object, int data2, Object object2) {
}
}
public class StringNumber {
private LinkedListNode<Integer> head;
private LinkedListNode<Integer> tail;
private int size;
// 尾部插入
public boolean addTail(int data) {
if (this.head == null) {
this.head = new LinkedListNode<Integer>(null, data, null);
this.tail = this.head;
} else {
LinkedListNode<Integer> newnode = new LinkedListNode<Integer>(this.tail, data, null);
this.tail.next = newnode;
this.tail = newnode;
}
this.size++;
return true;
}
public String toString() {
if (this.isEmpty())
return "[]";
else {
StringBuffer st = new StringBuffer("[");
for (LinkedListNode<Integer> curent = this.head; curent != null; curent = curent.next)
st.append(curent.data.toString() + " ,");
st.append("]");
return st.toString();
}
}
public boolean isEmpty() {
return this.size == 0;
}
public void deleteDups(LinkedListNode<Integer> n){
Hashtable<Integer, Boolean> table = new Hashtable<Integer, Boolean>();
LinkedListNode<Integer> previous = null;
while(n != null){
if(table.containsKey(n.data)){
previous.next = n.next;
}else{
table.put(n.data, true);
previous = n;
}
n =n.next;
}
while(head!=null){
System.out.println(head.data.toString());
head= head.next;
}
}
public void deleteDups2(LinkedListNode<Integer> head){
if(head == null)
return;
LinkedListNode<Integer> current = head;
while(current != null){
//移除後續值相同的全部節點
LinkedListNode<Integer> runner = current;
while(runner.next != null){
if(runner.next.data == current.data){
runner.next = runner.next.next;
}else{
runner = runner.next;
}
}
current= current.next;
}
while(head!=null){
System.out.println(head.data.toString());
head= head.next;
}
}
public static void main(String[] args){
StringNumber test = new StringNumber();
test.addTail(1);
test.addTail(2);
test.addTail(3);
test.addTail(3);
test.addTail(4);
test.addTail(1);
test.deleteDups2(test.head);
}
}
或者:
public class ListNote { private ListNote NextNote; private int value; public ListNote(){ } public ListNote(int value){ this.value=value; } public ListNote getNext(){ return NextNote; } public void setNext(ListNote next){ this.NextNote=next; } public int getValue(){ return value; } public void setValue(int value){ this.value=value; } }
public static void deleteDuplication(ListNote root){ if(root==null){ return; } ListNote preNode=null;//前結點 ListNote node=root;//當前結點 while(node!=null){ ListNote nextNode=node.getNext();//下一個結點 boolean needDelete=false; //判斷當前結點和下一個結點值是否相等 if(nextNode!=null&&nextNode.getValue()==node.getValue()) needDelete=true; if(!needDelete){//不相等,向前移動 preNode=node; node=node.getNext(); } else{//相等,刪除該結點 int value=node.getValue(); ListNote toBeDel=node; while(toBeDel!=null&&toBeDel.getValue()==value){ nextNode=toBeDel.getNext();//刪除該結點 toBeDel=nextNode; } if(preNode==null){//頭結點刪除時 root=nextNode; } else{ //即刪除了重複結點 preNode.setNext(nextNode); } node=nextNode; } } }