劍指offer第二版-18.2刪除排序鏈表中重復的節點

本系列導航:劍指offer(第二版)java實現導航帖

面試題18題目二:刪除排序鏈表中重復的節點

題目要求:
比如[1,2,2,3,3,3],刪除之后為[1];

解題思路:
由于是已經排序好的鏈表,需要確定重復區域的長度,刪除后還需要將被刪去的前與后連接,所以需要三個節點pre,cur,post,cur-post為重復區域,刪除后將pre與post.next連接即可。
此外,要注意被刪結點區域處在鏈表頭部的情況,因為需要修改head。

package structure;
/**
 * Created by ryder on 2017/6/13.
 */
public class ListNode<T> {
    public T val;
    public ListNode<T> next;
    public ListNode(T val){
        this.val = val;
        this.next = null;
    }
    @Override
    public String toString() {
        StringBuilder ret = new StringBuilder();
        ret.append("[");
        for(ListNode cur = this;;cur=cur.next){
            if(cur==null){
                ret.deleteCharAt(ret.lastIndexOf(" "));
                ret.deleteCharAt(ret.lastIndexOf(","));
                break;
            }
            ret.append(cur.val);
            ret.append(", ");
        }
        ret.append("]");
        return ret.toString();
    }
}
package chapter3;

import structure.ListNode;

/**
 * Created by ryder on 2017/7/7.
 * 刪除排序鏈表中的重復節點
 */
public class P122_deleteDuplicatedNode {
    public static ListNode<Integer> deleteDuplication(ListNode<Integer> head){
        if(head==null||head.next==null)
            return head;
        ListNode<Integer> pre = null;
        ListNode<Integer> cur = head;
        ListNode<Integer> post = head.next;
        boolean needDelete = false;
        while (post!=null){
            if(cur.val.equals(post.val)){
                needDelete = true;
                post=post.next;
            }
            else if(needDelete && !cur.val.equals(post.val)){
                if(pre==null)
                    head = post;
                else
                    pre.next=post;
                cur = post;
                post = post.next;
                needDelete = false;
            }
            else{
                pre = cur;
                cur = post;
                post = post.next;
            }
        }
        if(needDelete && pre!=null)
            pre.next = null;
        else if(needDelete && pre==null)
            head = null;
        return head;
    }
    public static void main(String[] args){
        ListNode<Integer> head = new ListNode<>(1);
        head.next= new ListNode<>(1);
        head.next.next = new ListNode<>(2);
        head.next.next.next = new ListNode<>(2);
        head.next.next.next.next = new ListNode<>(2);
        head.next.next.next.next.next = new ListNode<>(3);
        System.out.println(head);
        head = deleteDuplication(head);
        System.out.println(head);
    }
}

運行結果

[1, 2, 3]
[1, 2]
[2]
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 本系列導航:劍指offer(第二版)java實現導航帖 面試題18:刪除鏈表的節點 題目要求:在o(1)時間內刪除...
    ryderchan閱讀 1,536評論 0 4
  • 1 序 2016年6月25日夜,帝都,天下著大雨,拖著行李箱和同學在校門口照了最后一張合照,搬離寢室打車去了提前租...
    RichardJieChen閱讀 5,138評論 0 12
  • //leetcode中還有花樣鏈表題,這里幾個例子,冰山一角 求單鏈表中結點的個數----時間復雜度O(n)這是最...
    暗黑破壞球嘿哈閱讀 1,531評論 0 6
  • 1. 慈悲是你最好的武器。 2. 學佛就是在學做人而已。 3. 沈默是毀謗最好的答覆。 4. 你要感謝告訴你缺點的...
    Valentina1989閱讀 703評論 0 0
  • 在一個早晨醒來, 我為你寫下一句句誓言, 這些誓言, 重復著, 反復地重復著。 我用你為我準備的 軟軟的毛巾, 擦...
    二馬行空閱讀 641評論 1 4