leetcode 082 Remove Duplicates from Sorted List II
2016-05-07 22:42
459 查看
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers
from the original list.
For example,
Given
Given
Subscribe to see which companies asked this question
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.
Subscribe to see which companies asked this question
/** * 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) { ListNode *l = head, *pre=head, *ret=NULL, *cur=NULL; if(head == NULL) return head; int cnt = 1; for(ListNode *next=l->next; next!=NULL; next=next->next) { if(pre->val == next->val) { cnt++; pre = next; continue; } if(cnt == 1) { if(ret == NULL) { ret = pre; }else { cur->next = pre; } cur = pre; cur->next=NULL; //cout << cur->val << endl; } cnt = 1; pre = next; } if(cnt == 1) { if(ret==NULL) { ret=pre; } else { cur->next=pre; } cur=pre; cur->next=NULL; } return ret; } };
相关文章推荐
- 最小生成树【Kruskal】POJ2377,POJ1258,poj2358,hdu1233
- Android Assests目录遍历
- 各种排序算法(冒泡、选择、快排、插入、希尔、堆排、归并、计数、基数)
- 20150309+Linux+LAMP安装-01
- 对所有对象都通用的方法
- SSO单点登录
- ZOJ 3946 Highway Project 贪心+最短路
- 更新条目时出错
- 存储班长信息的学生类
- 学习PS基础教程
- js基础01
- c++实验5-数组选择
- CSharpGL(22)实现顺序无关的半透明渲染(Order-Independent-Transparency)
- Linux防火墙iptables简明教程
- Mybatis入门程序
- Python 时间处理函数
- linux网卡绑定 网卡bond
- 你很有可能在滥用Docker
- 部分js代码
- Supervised Descent Method and its Applications to Face Alignment