# Description

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->4->5, return 1->2->5.

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

The original problem is here.

The original code is here.

# My Solution

I solve this problem in C++, as below:

``````/*
*Remove Duplicates from Sorted List II
*Author: shuaijiang
*Email: zhaoshuaijiang8@gmail.com
*/
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;

/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
if(node != NULL && node->val == headVal){
while(true){
while(node != NULL && node->val == headVal)
node = node->next;
if(node == NULL){
}
break;
}
}

while(currNode != NULL){
nextNode = currNode->next;
int currVal = currNode->val;
if(nextNode!=NULL && currVal == nextNode->val){
while(true){
while(nextNode!=NULL && currVal == nextNode->val){
nextNode = nextNode->next;
}

currNode = nextNode;
if(currNode == NULL)
break;
nextNode = currNode->next;
currVal = currNode->val;
if(nextNode==NULL || currVal != nextNode->val)
break;
}
lastNode->next = currNode;
lastNode = currNode;
if(currNode != NULL)
currNode = currNode->next;
}
else{
lastNode = currNode;
currNode = currNode->next;
}
}