[LeetCode] Remove Duplicate from Sorted Linkded List II
来源:互联网 发布:mac上邮箱怎么设置 编辑:程序博客网 时间:2024/04/29 18:06
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) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. if(head==NULL) return head; ListNode *p1=NULL; ListNode *p2=head; while(p2!=NULL) { if(p2->next==NULL || (p2->next!=NULL && p2->val!=p2->next->val) ) { if(p1==NULL) { p1=new ListNode(0); p1=p2; head=p1; } else{ p1->next=p2; p1=p2; } p2=p2->next; //if(p2!=NULL && p2->next!=NULL) // p1->next=NULL; continue; } int dup=p2->val; while(p2!=NULL && p2->val==dup) { if(p1!=NULL) p1->next=NULL; ListNode *tmp=p2; p2=p2->next; delete tmp; } } if(p1==NULL) return NULL; return head; }};
- [LeetCode] Remove Duplicate from Sorted Linkded 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
- [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
- [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
- Leetcode: Remove Duplicates from Sorted List II
- leetcode Remove Duplicates from Sorted List II
- C语言中的宏定义
- HDU 4099 Revenge of Fibonacci(高精度+字典树)
- 优化 Flash 性能
- git - 简易指南
- B. Eight Point Sets
- [LeetCode] Remove Duplicate from Sorted Linkded List II
- AJAX 跨域请求 - JSONP获取JSON数据
- OCP-1Z0-052-V8.02-44题
- 关于64位 Ubuntu 13.04 安装Android Studio的一些问题及ADB驱动配置
- C++中private继承
- RPM的安装
- 无线网卡的Master,Managed,ad-hoc,monitor等模式
- dbms_space 显示自动段顾问建议!
- Dbutils 多表联合查询不支持别名解决方法