[leetcode] Remove Duplicates from Sorted List II
2015-07-13 19:00
381 查看
From : https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given
Given
Hide Tags
Linked List
Solution :
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.
Hide Tags
Linked List
Solution :
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* deleteDuplicates(ListNode* head) { if(!head) return NULL; int v = head->val; ListNode *th = new ListNode(0), *pre, *p; th->next = head; pre = th; p = head; while(p) { if(p->next && p->next->val == v) { while(p && p->val == v) { pre->next = p->next; delete p; p = pre->next; } } else { pre = p; p = p->next; } if(p) v=p->val; } p = th->next; delete th; return p; } };
相关文章推荐
- HDU 4463 Outlets 最小生成树Kr~
- Zoj 2562 More Divisors (反素数)
- 6:ZigZag Conversion
- ubuntu 修改主机名
- commons-fileupload上传文件(1)
- Swiper使用方法(向前和向后按钮在swiper-container外面)
- [LeetCode] Number of 1 Bits
- Linux监控工具vmstat命令详解
- scikit-learn:CountVectorizer提取tf都做了什么
- 自定义文本横向渐变消失的控件
- 一些随机噪波函数(随时更新)
- Android清单文件详解(一) ---- 一切从<manifest>开始
- c++ primer读书笔记-第十四章 重载操作符与转换
- Hadoop实现远程登录与调试
- ceph 数据校验机制 scrub源码分析
- mvvm 模板中事件没有执行的解决方案
- SQL查询数据库是否存在
- 【 D3.js 入门系列 — 3 】 做一个简单的图表!
- Android网络请求框架Volley的使用
- 平安科技移动开发二队技术周报(第十期)