Topic:
Given a sorted linked list, delete all duplicates such this each element appear only once.
For example,
Given1->1->2
, return1->2
.
Given1->1->2->3->3
, return1->2->3
.
Idea: and array deletions are very similar
#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* low= Head; list* fast = head->next; list* tmp;while (Fast! = NULL) {if (Low->value = = fast->value) {tmp = Fast;fast = fast->next;low-≫next = Fast;delete tmp;} Else{low = Low->next;fast = Fast->next;}}} int main () {int array[]={1,1,1,2,3,3,4,5,6,6,7,7}; list* head;init_list (head,array,sizeof (array)/sizeof (int)); Removeduplicate (head);p rint_list (head); return 0;}
Remove Duplicates from Sorted List--leetcode