【劍指Offer學習】【面試題57:刪除鏈表中重複的結點】,劍指offer

來源:互聯網
上載者:User

【劍指Offer學習】【面試題57:刪除鏈表中重複的結點】,劍指offer
題目:在一個排序的鏈表中,如何重複資料刪除的結點?解題思路

  解決這個問題的第一步是確定刪除的參數。當然這個函數需要輸入待刪除鏈表的頭結點。頭結點可能與後面的結點重複,也就是說頭結點也可能被刪除,所以在鏈表頭添加一個結點。
  接下來我們從頭遍曆整個鏈表。如果當前結點的值與下一個結點的值相同,那麼它們就是重複的結點,都可以被刪除。為了保證刪除之後的鏈表仍然是相連的而沒有中間斷開,我們要把當前的前一個結點和後面值比當前結點的值要大的結點相連。我們要確保prev要始終與下一個沒有重複的結點串連在一起。

結點定義
private static class ListNode {    private int val;    private ListNode next;    public ListNode() {    }    public ListNode(int val) {        this.val = val;    }    @Override    public String toString() {        return val + "";    }}
代碼實現
public class Test57 {    private static class ListNode {        private int val;        private ListNode next;        public ListNode() {        }        public ListNode(int val) {            this.val = val;        }        @Override        public String toString() {            return val + "";        }    }    private static ListNode deleteDuplication(ListNode head) {        // 為null        if (head == null) {            return null;        }//        // 只有一個結點//        if (head.next == null) {//            return head;//        }        // 臨時的頭結點        ListNode root = new ListNode();        root.next = head;        // 記錄前驅結點        ListNode prev = root;        // 記錄當前處理的結點        ListNode node = head;        while (node != null && node.next != null) {            // 有重複結點,與node值相同的結點都要刪除            if (node.val == node.next.val) {                // 找到下一個不同值的節點,注意其有可能也是重複節點                while (node.next != null && node.next.val == node.val) {                    node = node.next;                }                // 指向下一個節點,prev.next也可能是重複結點                // 所以prev不要移動到下一個結點                prev.next = node.next;            }            // 相鄰兩個值不同,說明node不可刪除,要保留            else {                prev.next = node;                prev = prev.next;            }            node = node.next;        }        return root.next;    }    private static void print(ListNode head) {        while (head != null) {            System.out.print(head + "->");            head = head.next;        }        System.out.println("null");    }    public static void main(String[] args) {        test01();        test02();        test03();        test04();        test05();        test06();        test07();        test08();        test09();        test10();    }    // 1->2->3->3->4->4->5    private static void test01() {        ListNode n1 = new ListNode(1);        ListNode n2 = new ListNode(2);        ListNode n3 = new ListNode(3);        ListNode n4 = new ListNode(3);        ListNode n5 = new ListNode(4);        ListNode n6 = new ListNode(4);        ListNode n7 = new ListNode(5);        n1.next = n2;        n2.next = n3;        n3.next = n4;        n4.next = n5;        n5.next = n6;        n6.next = n7;        ListNode result = deleteDuplication(n1);        print(result);    }    // 1->2->3->4->5->6->7    private static void test02() {        ListNode n1 = new ListNode(1);        ListNode n2 = new ListNode(2);        ListNode n3 = new ListNode(3);        ListNode n4 = new ListNode(4);        ListNode n5 = new ListNode(5);        ListNode n6 = new ListNode(6);        ListNode n7 = new ListNode(7);        n1.next = n2;        n2.next = n3;        n3.next = n4;        n4.next = n5;        n5.next = n6;        n6.next = n7;        ListNode result = deleteDuplication(n1);        print(result);    }    // 1->1->1->1->1->1->2    private static void test03() {        ListNode n1 = new ListNode(1);        ListNode n2 = new ListNode(1);        ListNode n3 = new ListNode(1);        ListNode n4 = new ListNode(1);        ListNode n5 = new ListNode(1);        ListNode n6 = new ListNode(1);        ListNode n7 = new ListNode(2);        n1.next = n2;        n2.next = n3;        n3.next = n4;        n4.next = n5;        n5.next = n6;        n6.next = n7;        ListNode result = deleteDuplication(n1);        print(result);    }    // 1->1->1->1->1->1->1    private static void test04() {        ListNode n1 = new ListNode(1);        ListNode n2 = new ListNode(1);        ListNode n3 = new ListNode(1);        ListNode n4 = new ListNode(1);        ListNode n5 = new ListNode(1);        ListNode n6 = new ListNode(1);        ListNode n7 = new ListNode(1);        n1.next = n2;        n2.next = n3;        n3.next = n4;        n4.next = n5;        n5.next = n6;        n6.next = n7;        ListNode result = deleteDuplication(n1);        print(result);    }    // 1->1->2->2->3->3->4->4    private static void test05() {        ListNode n1 = new ListNode(1);        ListNode n2 = new ListNode(1);        ListNode n3 = new ListNode(2);        ListNode n4 = new ListNode(2);        ListNode n5 = new ListNode(3);        ListNode n6 = new ListNode(3);        ListNode n7 = new ListNode(4);        ListNode n8 = new ListNode(4);        n1.next = n2;        n2.next = n3;        n3.next = n4;        n4.next = n5;        n5.next = n6;        n6.next = n7;        n7.next = n8;        ListNode result = deleteDuplication(n1);        print(result);    }    // 1->1->2->3->3->4->5->5    private static void test06() {        ListNode n1 = new ListNode(1);        ListNode n2 = new ListNode(1);        ListNode n3 = new ListNode(2);        ListNode n4 = new ListNode(3);        ListNode n5 = new ListNode(3);        ListNode n6 = new ListNode(4);        ListNode n7 = new ListNode(5);        ListNode n8 = new ListNode(5);        n1.next = n2;        n2.next = n3;        n3.next = n4;        n4.next = n5;        n5.next = n6;        n6.next = n7;        n7.next = n8;        ListNode result = deleteDuplication(n1);        print(result);    }    // 1->1->2->2->3->3->4->5->5    private static void test07() {        ListNode n1 = new ListNode(1);        ListNode n2 = new ListNode(1);        ListNode n3 = new ListNode(2);        ListNode n4 = new ListNode(2);        ListNode n5 = new ListNode(3);        ListNode n6 = new ListNode(3);        ListNode n7 = new ListNode(4);        ListNode n8 = new ListNode(5);        ListNode n9 = new ListNode(5);        n1.next = n2;        n2.next = n3;        n3.next = n4;        n4.next = n5;        n5.next = n6;        n6.next = n7;        n7.next = n8;        n8.next = n9;        ListNode result = deleteDuplication(n1);        print(result);    }    // 1->2    private static void test08() {        ListNode n1 = new ListNode(1);        ListNode n2 = new ListNode(2);        n1.next = n2;        ListNode result = deleteDuplication(n1);        print(result);    }    // 1    private static void test09() {        ListNode n1 = new ListNode(1);        ListNode result = deleteDuplication(n1);        print(result);    }    // null    private static void test10() {        ListNode result = deleteDuplication(null);        print(result);    }}
運行結果

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.