Leetcode82:remove duplicates from Sorted List II

Source: Internet
Author: User
Tags prev
Given a sorted linked list, delete all nodes that has duplicate numbers, leaving only 
distinct numbers from the Origi NAL list.

For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.
Solution One

My idea is that for a node of a linked list, we first find the duplicate elements of the node, then delete the duplicate elements, and then iterate over the linked list. The idea of first querying a repeating element and then deleting it requires three pointers, and there are different ways of dealing with duplicate elements in the chain header and duplicate elements in the middle of the linked list.
Consider the existence of duplicate elements in the chain header first: [1,1,1,2,3]:
Use three pointers First,second,third. First initially points to the element, second and Thrid point to the second element.
For cases where duplicate elements exist in the list header, only two pointers are used, where first and second are used

Compare the elements that first and second point to if they are equal, and then move the second to the right until you find an element that is not equal to the one that is pointed to. The following figure:

The element is then removed from the [First,second] and the first point points to the same element second. At this point, if first is empty, it means that there are no elements behind the list, both second and third are null, and if first is not empty, second and third are moved forward one bit. You can then return to the while loop and then judge. The reason for using while without if is to handle such a test case: [1,1,2,2,3].

The

Above is the processing of duplicate elements that exist in the linked header. When the
is processed, the elements pointed to by first and second (second and third are initially pointed to the same element) have different values. The point here is to judge how many of the second are behind if there are duplicate elements, and this is where the third pointer comes in handy. This pointer is used to point to the last node with the second pointer pointing to the same value of the node, for [1,2,2,2,3] This test case:

Third the initial and second point to the same element, which is a node behind first, Because a node with the same value as the second element exists behind second, the third pointer is moved back to the last node with the same value as the second element. The next pointer to first is pointed to the next node of Thrid, and then the elements between second and third are removed. Then update the location of second and third.
C + + code implementation:
Runtime:11ms

/** * Definition for singly-linked list.
 * struct ListNode {* int val;
 * ListNode *next;
 * ListNode (int x): Val (x), Next (NULL) {}};

    */class Solution {public:listnode* deleteduplicates (listnode* head) {if (head==null) return NULL; Using three pointers, the first pointer points to the last element that is not duplicated, the second pointer and the third pointer indicate a repeating area of the number//initial first pointer to the linked header, the second second and the third second pointer to the second element ListNode *
    First=head;
    ListNode *second=head->next;

    ListNode *third=head->next;

    Determine if there is a duplicate variable bool repeat=false during traversal; Special handling is required for the first duplicate element in the array//At this point when it is necessary to compare first and second points for equality, you need to make a non-null judgment//process for both of them in the previous test case [1,1,2,2,3] while (first!= Null&&second!=null&&first->val==second->val) {//If the first and second elements are equal, you need to continue querying until you find the first element, Second points at this point while (First!=null&&second!=null&&first->val==second->val) second=sec

        ond->next;
        It then begins to delete the element until second, when the first pointer and second pointer point to the same node ListNode *tmp=first; While(Tmp!=second)
            {first=first->next;
            Delete tmp;
        Tmp=first; }//Then judge the node pointed to above first and second, if it is not empty, then second and third move backwards one if (first!=null) {second=f
            irst->next;
        Third=second;
            } else//If the node is empty, indicating that no elements are present, the second and third pointers are placed as NULL {second=null;
        Third=null;
    }}//At the end of the loop above you can determine the head pointer of the linked list after the element is deleted and save it.

    Head=first; It then starts traversing the list while (Third!=null) {//determines if there are duplicate elements behind the first pointer, compares the next element of the third pointer to the element pointed to by the second pointer,//until a different element is found, The second element points to the first element of a repeating element,//third points to the last element of the repeating element, and records the existence of a repeating element after the next first element while (Third!=null&&third->next
            !=null&&second->val==third->next->val) {third=third->next;
        Repeat=true;
        }//If there are duplicate elements behind first, you need to remove the intermediate elements pointed to by the second and third pointers. if (repeat) {//First pointer to the next element of third, this element must be the SECThe elements in the middle of ond and third are different first->next=third->next;
            Start removing elements between [Second,third], including second and third ListNode *tmp=second;
                while (Second!=third->next) {second=second->next;
                Delete tmp;
            Tmp=second;
            }//Then move the second pointer back one bit and keep the second and third the same second=first->next;

            Third=second;
        After processing the repeating element, it is necessary to reset the variable of the repeating element to false repeat=false;
            } else//if the element behind first is not a repeating element, then first plus 1, second and third point to the next element {First=second;
            second=second->next;
        Third=second;
}} return head; }
};
Solution Two

See a lot of ways to use a pseudo-node in the discuss, using this pseudo-node can be in the chain header above the existence of duplicate elements and the intermediate existence of duplicate elements together, which can reduce a lot of code. And you can merge the duplicate query elements with the delete operation (in fact, the first query and then delete is a very foolish way, do not make this silly later). This makes the code very concise.
Note that the following Fakenode node is a pseudo-node, and the Prev pointer points to the previous non-repeating node of the current node. According to the meaning of prev, it can be found that Prev is Fakenode node initially. Because the Fakenode node initially points to the head node.
Runtime:8ms

/** * Definition for singly-linked list.
 * struct ListNode {* int val;
 * ListNode *next;
 * ListNode (int x): Val (x), Next (NULL) {}};
    */class Solution {public:listnode* deleteduplicates (listnode* head) {ListNode * fakenode=new listnode (0);
    fakenode->next=head;
    ListNode *prev=fakenode; while (Head!=null) {if (head->next&&head->val==head->next->val) {i
                NT value=head->val;
                    while (Head&&head->val==value) {prev->next=head->next;
                    Delete head;
                head=prev->next;
            }} else {prev=head;
        head=head->next;
}} return fakenode->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.