Remove Duplicates from Sorted List II
来源:互联网 发布:搜狗小说书架数据恢复 编辑:程序博客网 时间:2024/06/10 16:49
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 1->2->3->3->4->4->5
, return 1->2->5
.
Given 1->1->1->2->3
, return 2->3
.
从前向后遍历,使用三个指针进行操作,画图理解
代码:
/** * 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==NULL) return NULL; if (head->next==NULL) return head; ListNode* first,*second; ListNode* prehead=new ListNode(0); prehead->next=head; first=head; second=first->next; int flag=0; ListNode* prefirst=prehead; while(second) { if (first->val==second->val) { flag=1; prefirst->next=second; first=second; second=second->next; } else { if (flag) { prefirst->next=second; first=second; second=second->next; } else { prefirst=first; first=second; second=second->next; } flag=0; } } if (flag) { prefirst->next=NULL; } return prehead->next; }};
阅读全文
0 0
- LeetCode: Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List II
- LeetCode: Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List II
- [leetcode] Remove Duplicates from Sorted List II
- [LeetCode] Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- [leetcode] Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- Remove duplicates from sorted list II
- Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- [LeetCode]Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- [leetcode]Remove Duplicates from Sorted List II
- 【转载】解决IE6下的行高line-height失效问题
- 【转载】CSS样式对背景图片加载的影响
- 自定义View的一次简单尝试
- 2015年05月23日
- 2015年05月28日
- Remove Duplicates from Sorted List II
- 2015年06月01日
- js中md5加密
- 解决JS浮点数(小数)计算加减乘除的BUG
- 本周任务,对javascript事件机制进行思考 提供以下 几个博文 论点:
- js 将json字符串转换为json对象的方法解析
- pc端屏幕自适应小结
- 移动端安卓手机不能识别border 0.5px解决方案
- 分享下多年积累的对JAVA程序员成长之路的总结