-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveDuplicatesfromSortedListII.cpp
More file actions
executable file
·62 lines (56 loc) · 1.33 KB
/
RemoveDuplicatesfromSortedListII.cpp
File metadata and controls
executable file
·62 lines (56 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
// RemoveDuplicatesfromSortedListII.cpp
// leetcode
//
// Created by witwolf on 4/25/15.
// Copyright (c) 2015 witwolf. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
/**
* Definition for singly-linked list.
*/
struct ListNode {
int val;
struct ListNode *next;
};
void freeRange(struct ListNode* from,struct ListNode* to){
struct ListNode* node ;
while (from != to) {
node = from;
from = from->next;
free(node);
}
}
struct ListNode* deleteDuplicates(struct ListNode* head) {
struct ListNode* start_prev = NULL,*start=head,*end = NULL,*node = head;
while (node) {
if(node->val != start->val){
// 删除 [start,end]
if(start != end && end != NULL){
if(start == head){
head = node;
}else{
start_prev->next = node;
}
freeRange(start, node);
}else{
start_prev = start;
}
end = NULL;
start = node;
}else{
end = node;
}
node = node->next;
}
if(start != end && end!= NULL){
freeRange(start,NULL);
if(start_prev){
start_prev->next = NULL;
}else{
return NULL;
}
}
return head;
}