Leetcode# 82 Remove Duplicates from Sorted List II
2015-08-02 00:50
375 查看
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given
Given
Difficulty: Medium
对链表还是很不熟悉,语言也不够简练,今后改进!
For example,
Given
1->2->3->3->4->4->5, return
1->2->5.
Given
1->1->1->2->3, return
2->3.
Difficulty: Medium
对链表还是很不熟悉,语言也不够简练,今后改进!
ListNode* deleteDuplicates(ListNode* head) { if(head==NULL) return head; if(head->next==NULL) return head; else if(head->next->next==NULL&&head->val!=head->next->val) return head; else if(head->next->next==NULL&&head->val==head->next->val) { head = NULL; return head; } int flag = 0; ListNode* head1 = NULL; ListNode* pre = NULL; ListNode* p = head; ListNode* pNext; while(p->next->next!=NULL) { pNext = p->next; if(p->val==pNext->val&&pNext->val!=pNext->next->val) p = pNext->next; else if(p->val==pNext->val&&pNext->val==pNext->next->val) p = pNext; else if(p->val!=pNext->val) { if(flag==0){ head1 = new ListNode(p->val); flag = 1; pre = head1; p = pNext; } else{ ListNode* curr = new ListNode(p->val); pre->next = curr; pre = curr; p = pNext; } } if(p->next==NULL) { ListNode* curr = new ListNode(p->val); if(flag==1) { pre->next = curr; pre = curr; } else { head1 = curr; } return head1; } } if(p->val!=p->next->val){ pNext = p->next; ListNode* curr = new ListNode(p->val); if(flag==1){ pre->next = curr; pre = curr; curr->next = pNext; } else{ head1 = curr; head1->next = pNext; } return head1; } return head1; }
相关文章推荐
- spring事物传播属性
- Caf音频文件混合
- Lombok(1.14.8) - @Log
- 【PHP】php通过javascript判断不同设备分辨率来调用不同的css class
- hdu3068 最长回文(manacher 算法)
- Android Studio not find in root project android()
- 汇编程序--文件操作
- 快速排序
- 黑马程序员——collections类介绍和集合特殊的操作
- 数据库的备份和还原
- 线程管理之获取和设置线程信息
- hdu1209(Clock)
- c++_benchMark_vector_list_deque
- Replication_Error:Relay log write failure:could not queue event from master
- Activity技巧三重奏之一:知晓当前是在哪一个活动
- mysql-python安装时EnvironmentError: mysql_config not found
- GUI做出渐进的颜色变化(随机指定颜色)
- hdu_2817_快速幂
- 协作翻译平台 调研 version 0.5
- 五毛的cocos2d-x学习笔记08-动画