# Remove Duplicates from Sorted List II

639人阅读 评论(0)

## 题目描述

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

Example

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

## 链接地址

http://www.lintcode.com/en/problem/remove-duplicates-from-sorted-list-ii/

## 解法

 ListNode * deleteDuplicates(ListNode *head) { // write your code here if (head == NULL) { return head; } ListNode *dummyNode = new ListNode(); dummyNode->next = head; ListNode *pre = dummyNode; ListNode *cur = pre->next; while (cur!= NULL && cur->next!= NULL) { if (cur->val == cur->next->val) { ListNode *temp = cur; while (temp != NULL && temp->next != NULL && temp->val == temp->next->val) { ListNode *del = temp->next; temp->next = del->next; delete del; } pre ->next = temp->next; cur = pre->next; delete temp; } else { pre = cur; cur = cur->next; } } ListNode *ret = dummyNode->next; delete dummyNode; return ret; } 

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：319072次
• 积分：5120
• 等级：
• 排名：第5388名
• 原创：196篇
• 转载：4篇
• 译文：0篇
• 评论：11条
阅读排行
评论排行
最新评论