remove-duplicates-from-sorted-list
来源:互联网 发布:域名对应ip查询 编辑:程序博客网 时间:2024/06/12 20:20
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given1->1->2, return1->2.
Given1->1->2->3->3, return1->2->3.
开始用的方法不正确,每次在while循环里,先用temp给pre赋值,temp再后移,这样不行的原因是如果出现连续三个相同的节点会只删除一个。注意下面代码里的if和else。
代码:
/** * 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 *temp = head->next; ListNode *pre = head; while(temp!=NULL){ if(pre->val==temp->val){ pre->next = temp->next; temp = pre->next; } else{ pre = pre->next; temp = temp->next; } } 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
- 【NP-完全问题】课后习题8.3
- shell字符串匹配
- 解决Word中没有Endnote插件
- Python asyncio文档翻译--asyncio.Future
- centos6安装mariadb10全过程
- remove-duplicates-from-sorted-list
- 现在有空了,回来解决四年前uboot 读写fat 分区的一个txt文件问题
- text-stroke和text-fill-color的理解
- php+memcached CURD操作
- java分布式的介绍
- 文本中的单词切割
- 获取键盘按键值/查看该键对应的值
- tensorflow 单机多GPU 实例
- Spark排错与优化