Remove duplicates from Sorted List Ii--leetcode

Source: Internet
Author: User

Topic:

Given a sorted linked list, delete all nodes that has duplicate numbers, leaving only distinct numbers from the Original list.

For example,
Given1->2->3->3->4->4->5, return1->2->5.
Given1->1->1->2->3, return2->3.

Idea: First lock the head, then handle the middle position, remember the last processing tail, knowledge cumbersome. When handling the head. Find a node, the current node does not have the same continuous node, the same time node and post-sequence nodes are different. In the middle of processing, only the pre-order node and the traversal node are required. So that the traversal node does not have continuous same node. Assuming that the same node is contiguous, delete all successive same nodes

#include <iostream> #include <vector>using namespace std;typedef struct List_node list;struct list_node{int Value;struct list_node* next;};      void Init_list (list*& head,int* array,int n) {head = NULL;      list* tmp;      list* record;          for (int i=1;i<=n;i++) {tmp = new List;          Tmp->next = NULL;          Tmp->value = Array[i-1];              if (head = = NULL) {head = tmp;          record = head;              } else {record->next = tmp;          record = tmp;      }}} void Print_list (list* list) {list* tmp=list;          while (tmp! = NULL) {cout<<tmp->value<<endl;       TMP = tmp->next; }} void Removeduplicate (list*& head) {if (head = = NULL | | head->next==null) return; list* Pre=head; list* cur; List* Fast; if (Head->value = = Head->next->value) {while (1) {while (pre! = NULL &&pre->next! = NULL{if (Pre->value = = pre->next->value) Pre = pre->next; else break;} pre = pre->next;  if (Pre->next = = NULL | | Pre->value! = pre->next->value) {head = pre; break;} }} if (head = = NULL) return; Pre = head; cur=head->next; while (cur!=null && cur->next! = NULL) {if (Cur->value! = Cur->next->value) {if (Pre->next! = cur)  nonadjacent {cur = cur->next; pre->next = cur;} else{pre = Cur;cur = Cur->next;} } else {cur = cur->next;}} if (pre->next!=null && pre->next->next!=null) {if (Pre->next->value = = pre->next->next-   >value) Pre->next = NULL;}} int main () {int array[]={1,1,1,2,2,3,4,4,4,5,5,7,7,8}; list* head;init_list (head,array,sizeof (array)/sizeof (int)); Removeduplicate (head);p rint_list (head); return 0;}
PS: See each topic, first as far as possible to consider a comprehensive, and to consider how to use a more concise idea to solve the problem, in this topic, first of all, we need to consider the beginning of a non-eligible node, then the first step to deal with is to find the head node Finally, and then resolve the deletion node, Then at the time of deletion, consider two nodes at a time. Consider whether the two adjacent nodes are the same, assuming the same. This is the process of iterating over the current node value. If the assumptions are different, then the first node needs to be inserted into the last linked list.

Remove duplicates from Sorted List Ii--leetcode

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.