82. Remove Duplicates from Sorted List II
来源:互联网 发布:人工智能国内强的高校 编辑:程序博客网 时间:2024/05/29 03:15
problem:
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
.
class Solution {public: ListNode* deleteDuplicates(ListNode* head) { ListNode* result = new ListNode(0); if(head == NULL || head->next == NULL) return head; int flag = 0; int last; ListNode* index = head; ListNode* resultindex = new ListNode(0); if(head->val != head->next->val) { result->next = index; last = index->val; index = index->next; result->next->next = NULL; resultindex = result->next; flag++; } else last = index->val; while(index != NULL) { if(index->val != last) { ListNode* nextnode = index->next; if(nextnode != NULL && index->val == nextnode->val) { last = index->val; index = index->next; } else { if(flag == 0) { result->next = index; index = index->next; if(index != NULL) result->next->next = NULL; resultindex = result->next; flag++; } else { last = index->val; resultindex->next = index; index = index->next; resultindex = resultindex->next; resultindex->next = NULL; } } } else { index = index->next; } } return result->next; }};
0 0
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- Android 简易的音乐播放器
- Golang 代码程序案例
- 平行空间--打造自己的开场动画
- 论文中常用英语表达
- 高流量的网站,并不赞成使用集成框架
- 82. Remove Duplicates from Sorted List II
- C语言中空格符、空字符、字符数组结束符、换行、回车的区别
- Python学习--15 日期和时间
- Android中对象池的使用
- 关于引发异常的问题(异常名后,实例)
- Eclipse集成Maven
- sublime对齐
- eclipse:The specified Tomcat installation directory does not exist.
- ThinkPHP中session函数的参数中问号?开头(session('?name');)的意思