Remove Duplicates from Sorted List
来源:互联网 发布:excel数据分列怎么用 编辑:程序博客网 时间:2024/05/20 21:47
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
第一次写的可能还有错误的代码:
For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
相比第一次写的代码,这次的看起来有点代码之美的感觉,尤其是第一句if处理空和一个节点的情况。看看之前的代码,只处理了空的情况,结果后面的程序逻辑处理起来很繁琐,又很容易出错,代码质量太差了。第二次的代码出现了几个小错误,都是马虎所致。首先是if返回null,这是不对的,应该返回head,其次是把if里面的||写成了&&,导致oj报RTE,真的是太不细心了,离一次写完bugfree还差很远。
ListNode *deleteDuplicates(ListNode *head) {if (head == NULL || head->next == NULL) return head;ListNode *pre = head; ListNode *curr = head->next;while(curr != NULL){if (pre->val == curr->val){pre->next = curr->next;curr = curr->next;}else{pre = pre->next;curr = curr->next;}}return head;}
第一次写的可能还有错误的代码:
//ListNode *deleteDuplicates(ListNode *head) //{//if (head == NULL) return head;////ListNode *current = head;//ListNode *lastUnDup = head;////while (current!=NULL && current->next!=NULL)//{//if (current->val != current->next->val)//{//lastUnDup->next = current->next;//lastUnDup = current;//}//////}//return head; //}
0 0
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove duplicates from sorted list
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- b2DebugDraw到底是什么
- 直接拿来用!最火的Android开源项目
- Codeforces Round #127 (Div. 2) A. LLPS
- sublime text 2 中文乱码解决办法
- Apache限制下载线程数与带宽详解
- Remove Duplicates from Sorted List
- 1.简单工厂模式
- jenkins Build periodically 参数配置
- 14个办公室减肥小妙招 快速瘦身
- 来电归属地数据查询Java实现
- 办公室减肥小妙招
- 办公室减肥操
- XAMPP的常用路径
- C# 生成数据字典(Word 表格)