Package crack

Source Code of crack.Solution2_1

package crack;

import leetcode.ListNode;

public class Solution2_1 {
    public ListNode deleteDup(ListNode head) {
        ListNode first = head;
        while (first != null) {
            ListNode pre = first;
            ListNode cur = first.next;
            while(cur != null) {
                if (cur.val == first.val)
                    pre.next = cur.next;
                else
                    pre = cur;
                cur = cur.next;
            }
            first = first.next;
        }
        return head;
    }

    public void print(ListNode head) {
        while(head != null) {
            System.out.print(head.val + " ");
            head = head.next;
        }
        System.out.println();
    }

    public ListNode construct(int[] arr) {
        ListNode head = null;
        ListNode cur  = null;
        for (int x : arr) {
            if (cur == null) {
                head = cur = new ListNode(x);
            } else {
                cur.next = new ListNode(x);
                cur = cur.next;
            }
        }
        return head;
    }

    public static void main(String args[]) {
        Solution2_1 so = new Solution2_1();
        ListNode head = so.construct(new int[]{});
        so.print(so.deleteDup(head));
        head = so.construct(new int[]{1, 2, 3, 3 ,3, 2, 1, 2});
        so.print(so.deleteDup(head));
        head = so.construct(new int[]{1});
        so.print(so.deleteDup(head));
    }
}
TOP

Related Classes of crack.Solution2_1

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.