#include <bits/stdc++.h>
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
class Solution {
public:
ListNode* deleteDuplication(ListNode* pHead) {
if(pHead == nullptr)
return nullptr;
ListNode* pNewHead = new ListNode(0);
ListNode* curNode = pNewHead;
while (pHead != nullptr)
{
if(pHead->next == nullptr && pHead->val != pHead->next->val)
{
curNode->next = pHead;
curNode = curNode->next;
}
while (pHead->next != nullptr && pHead->val == pHead->next->val)
{
pHead = pHead->next;
}
pHead = pHead->next;
}
return pNewHead;
}
};
int main()
{
ListNode* pHead = new ListNode(1);
ListNode* pNode1 = new ListNode(2);
ListNode* pNode2 = new ListNode(3);
ListNode* pNode3 = new ListNode(3);
ListNode* pNode4 = new ListNode(4);
ListNode* pNode5 = new ListNode(4);
ListNode* pNode6 = new ListNode(5);
pHead->next = pNode1;
pNode1->next = pNode2;
pNode2->next = pNode3;
pNode3->next = pNode4;
pNode4->next = pNode5;
pNode5->next = pNode6;
Solution cSolution;
cSolution.deleteDuplication(pHead);
return 1;
}
package main
type ListNode struct{
Val int
Next *ListNode
}
func deleteDuplication( pHead *ListNode ) *ListNode {
if pHead == nil {
return nil
}
pNewHead := new(ListNode)
curNode := pHead
for pHead != nil{
if pHead.Next != nil && pHead.Val == pHead.Next.Val {
curNode.Next = pHead;
curNode = curNode.Next
}
for pHead.Next != nil && pHead.Val == pHead.Next.Val{
pHead = pHead.Next
}
pHead = pHead.Next
}
return pNewHead.Next
}