83. Remove Duplicates from Sorted List
来源:互联网 发布:亨通大数据产业园 编辑:程序博客网 时间:2024/06/05 06:01
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
.
给定一个已经排序好的链表,删除其中的重复元素。
设置两个指针,ListNode *p=head,ListNode *q=head->next,两个指针一前一后遍历整个链表,当发现重复元素时q=q->next,否则p->next=q,p=q,q=q->next;
class Solution
{
public:
ListNode* deleteDuplicates(ListNode* head)
{
ListNode* p=head;
ListNode* q=head->next;
while(q!=NULL)
{
if(p->val==q->val)
{
q=q->next;
}
else
{
p->next=q;
p=q;
q=q->next;
}
}
q->next=NULL;
return head;
}
};
阅读全文
0 0
- [LeetCode]83.Remove Duplicates from Sorted List
- 83. Remove Duplicates from Sorted List
- [Leetcode] 83. Remove Duplicates from Sorted List
- LeetCode --- 83. Remove Duplicates from Sorted List
- [leetcode] 83.Remove Duplicates from Sorted List
- 83. Remove Duplicates from Sorted List
- LeetCode 83. Remove Duplicates from Sorted List
- 83. Remove Duplicates from Sorted List
- 【leetcode】83. Remove Duplicates from Sorted List
- 83. Remove Duplicates from Sorted List
- 83. Remove Duplicates from Sorted List LeetCode
- 83. Remove Duplicates from Sorted List
- leetcode 83. Remove Duplicates from Sorted List
- 83. Remove Duplicates from Sorted List
- 83. Remove Duplicates from Sorted List
- [LeetCode]83. Remove Duplicates from Sorted List
- 【LeetCode】83. Remove Duplicates from Sorted List
- LeetCode 83. Remove Duplicates from Sorted List
- C语言基础之位运算
- python操作excel
- Django连接数据库,解决Django migrate No changes detected 不能创建表的问题
- imageloard的网络加载图片的详解
- SynchronousQueue的使用
- 83. Remove Duplicates from Sorted List
- ConcurrentHashMap源码剖析(1.8版本)
- 自我介绍
- MyBatis中如何使用PageHelper插件实现 分页
- 好玩的东西
- 人工智能A7论坛2017年迄今最新人工智能资源盘点
- 写给in4sec的粉丝和志同道合的小伙伴们
- [effectiv c++]条款25:考虑写出一个不抛出异常的swap函数
- 零基础学习C++(一)