Cracking the coding interview Q2.1

Source: Internet
Author: User

Write code to remove duplicates from an unsorted linked list.

Follow up

How wocould you solve this problem if a temporary buffer is not allowed?

 

Idea 1: Use hashset to store existing nodes. If there are already multiple nodes, delete them. Space O (N), time O (n ).

Idea 2: two pointers. For each current node, traverse the elements behind and delete duplicates. Space O (1), time O (N ^ 2 ).

Thought 3: Is the mobile window method feasible?

 

    public static void deleteDupsA(LinkedListNode n) {        HashSet<Integer> set = new HashSet<Integer>();        LinkedListNode previous = null;        while (n != null) {            if (set.contains(n.data)) {                previous.next = n.next;            } else {                set.add(n.data);                previous = n;            }            n = n.next;        }    }        public static void deleteDupsC(LinkedListNode head) {        if (head == null) return;        LinkedListNode previous = head;        LinkedListNode current = previous.next;        while (current != null) {            // Look backwards for dups, and remove any that you see.            LinkedListNode runner = head;            while (runner != current) {                 if (runner.data == current.data) {                    LinkedListNode tmp = current.next;                    previous.next = tmp;                    current = tmp;                    /* We know we can‘t have more than one dup preceding                     * our element since it would have been removed                      * earlier. */                    break;                }                runner = runner.next;            }                        /* If runner == current, then we didn‘t find any duplicate              * elements in the previous for loop.  We then need to              * increment current.               * If runner != current, then we must have hit the ?break?              * condition, in which case we found a dup and current has             * already been incremented.*/            if (runner == current) {                previous = current;                current = current.next;            }        }    }        public static void deleteDupsB(LinkedListNode head) {        if (head == null) return;                LinkedListNode current = head;        while (current != null) {            /* Remove all future nodes that have the same value */            LinkedListNode runner = current;            while (runner.next != null) {                 if (runner.next.data == current.data) {                    runner.next = runner.next.next;                } else {                    runner = runner.next;                }            }            current = current.next;        }    }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.