82. Remove Duplicates from Sorted List II My Submissions Question
2016-03-17 16:07
225 查看
<p style="margin-top: 0px; margin-bottom: 10px; box-sizing: border-box; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;">Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only <span style="box-sizing: border-box;">distinct</span> numbers from the original list.</p><p style="margin-top: 0px; margin-bottom: 10px; box-sizing: border-box; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;">For example,<br style="box-sizing: border-box;" />Given <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13.3333px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; background-color: rgb(249, 242, 244);">1->2->3->3->4->4->5</code>, return <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13.3333px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; background-color: rgb(249, 242, 244);">1->2->5</code>.<br style="box-sizing: border-box;" />Given <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13.3333px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; background-color: rgb(249, 242, 244);">1->1->1->2->3</code>, return <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13.3333px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; background-color: rgb(249, 242, 244);">2->3</code>.</p><p style="margin-top: 0px; margin-bottom: 10px; box-sizing: border-box; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;">solution:</p>
/*** 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 || head->next == NULL)return head;ListNode *pre = head;ListNode *cur = head->next;while(pre->val == cur->val){while(cur != NULL && cur->val == pre->val){cur = cur->next;}if(cur == NULL) return NULL;else{head = cur;if(cur->next == NULL) return cur;else{pre = cur;cur = cur->next;}}}while(cur->next != NULL){ListNode *p = cur->next;if(p->val == cur->val){while(p != NULL && p->val == cur->val){p = p->next;}if(p == NULL){pre->next = NULL;return head;}else{pre->next = p;cur = p;}}else{pre = pre->next;cur = cur->next;}}return head;}};心得: 思路简单速度:快
相关文章推荐
- TableView详细解释
- iOS --UIScrollView的学习(一)
- KVC中setValuesForKeysWithDictionary 崩溃问题
- UITableView刷新某一行或某一部分,并记录做项目时的一小心得
- 为什么子线程不能做ui操作
- ant之build.xml基本属性详解
- POJ2431 运用queueq优先队列
- android之handler messagequene looper threadlocal 之间的详细解读
- uipickerview用法
- @RequestBody, @ResponseBody 注解详解(转)
- AS-->如何更高效的使用 Gradle, 快速build apk
- phpQuery对数据信息的采集进一步学习
- @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
- Android Volley 之自定义Request
- 【笔记】《C#大学教程》- 第12章 GUI(一)
- document.getElementById("XXX").innerHTML与document.getElementById("XXX").value
- FineUI(专业版)v3.0.0 发布,手机、平板和桌面全支持!
- 关于UIButton setImage 不显示 和 setTitle不能和谐相处的问题
- 多字典同key时对value加法操作
- iOS 子视图 父视图 UIView 相关的方法