Leetcode 2. Remove duplicate elements from the ordered linked list and array Remove Duplicates, leetcodeduplicates

Source: Internet
Author: User

Leetcode 2. Remove duplicate elements from the ordered linked list and array Remove Duplicates, leetcodeduplicates
1. linked List, so that duplicate elements only appear once Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,
Given 1-> 1-> 2, return 1-> 2.
Given 1-> 1-> 2-> 3-> 3, return 1-> 2-> 3.

Ideas:
There are two methods to implement a linked list: recursive and non-recursive.
Recursive Implementation:

    ListNode* h = head;    if (h == NULL)        return NULL;    if (h->next != NULL && h->next->next != NULL && h->val == h->next->val)        h->next = h->next->next;    h->next = deleteDuplicates(h->next);    return h;

Because there may be more than three identical elements in a row, you need to consider the right shift of the pointer. In the following program, when elements 1 and 2 are equal, after the second element is deleted, the pointer shifts right. At this time, the first recursion starts with 3rd elements. If 3rd elements and 1st elements are equal, an error occurs. It is complicated to write correctly.

Non-Recursive Implementation:

/*** 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 * h = head; if (h = NULL) return NULL; ListNode * t = NULL; while (h! = NULL) {t = h; // reference pointer while (h = h-> next )! = NULL) // shift right of the loop until {if (t-> val = h-> val) continue; else break;} t-> next = h ;} return head ;}};

Leetcode result:
Submission Details
164/164 test cases passed.
Status: Accepted
Runtime: 17 MS

// Another implementation, delete one by one. prev serves as the reference item class Solution {public: ListNode * deleteDuplicates (ListNode * head) {if (head = NULL | head-> next = NULL) return head; ListNode * prev = head; ListNode * cur = head-> next; for (; cur; cur = cur-> next) {if (prev-> val = cur-> val) {prev-> next = cur-> next; delete cur ;} else {prev = cur ;}} return head ;}};
2 linked list, remove all repeated Elements

Problem: given an ordered linked list, all repeated elements are removed.
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

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

Ideas:

2.1 Non-recursive (iterative) version:
Note: The previous node of prev and the current node of cur

(1) You can intuitively think of introducing the node prev, but it is not easy to assign values to the prev node. First, when cur is set to head, what value should prev be assigned, if the head is also assigned, it is necessary to compare the ideas in the subsequent programs to make corrections. ThereforeIntroduce a virtual node dummy to make it the prev node of the head node.
(2) how to delete duplicate nodes: Chapter 1 associates it with section 1. If the whole block is removed from a duplicate node, it will affect the move back (cur = cur-> next) of the cur node, that is, all are removed, and the cur points to where the prev is moved. Therefore, we adoptRetain the initial reference items, delete the duplicate nodes after the reference items one by one, and finally remove the reference items..
(3) When will the prev node be moved back? When there are duplicate nodes, we will delete these nodes, so only when there are no duplicates, prev will move back, so thinkIntroduce flag bit-whether the tag already exists.
(4) Note that the last prev-> next = NULL; the prev link is the linked list after processing.

/** * 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 dummy(0);        if (head == NULL)             return NULL;        ListNode* cur = head;        ListNode* prev = &dummy;        dummy.next = head;        bool duplicate = false;        while (cur != NULL)        {            duplicate = false;            while (cur->next != NULL && cur->val == cur->next->val)            {                duplicate = true;                ListNode *tmp;                tmp = cur;                cur = cur->next;                delete tmp;            }            if (duplicate)            {                ListNode *tmp;                tmp = cur;                cur = cur->next;                delete tmp;                continue;            }            prev->next = cur;            prev = prev->next;            cur = cur->next;        }        prev->next = cur; //NULL        head = dummy.next;        return head;    }};
Submission Details166 / 166 test cases passed.Status: AcceptedRuntime: 14 msSubmitted: 2 minutes ago

Algorithm time complexity $ O (n) $, airborne complexity $ O (1) $
Conclusion: You must delete them one by one. You can introduce virtual nodes and flag spaces to mark the occurrence of some events.

2.2 recursive version

Idea: Recursive Implementation is hard to imagine,
For recursion, it can be divided into two types: Same and non-identical intervals, respectively, recursion
If the same element exists: the result of deleting all identical elements should be returned.
When there is no synchronization: move one by one, link different nodes together, and then return the header pointer

Class Solution {public: // recursive ListNode * deleteDuplicates (ListNode * head) {if (head = NULL | head-> next = NULL) return head; listNode * p = head-> next; if (head-> val = p-> val) {while (p! = NULL & head-> val = p-> val) // Delete the reference items one by one {ListNode * tmp; tmp = p; p = p-> next; delete tmp;} delete head; // delete reference item return deleteDuplicates (p ); // Delete the same result} else {head-> next = deleteDuplicates (head-> next ); // return head together with different nodes after the same deletion ;}}};

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.