您的位置:首页 > 编程语言 > C语言/C++

链表-leetcode 83 Remove Duplicates from Sorted List

2017-06-29 15:26 525 查看
原题链接:Remove Duplicates from Sorted List

分析:因为链表已经是有序的,所以重复的值都会集中在一起,所以直接遍历,删除重复值就行。

题解:

/**
* 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) {
/*
Time Complexity:O(N)
Space Complexity:O(1)
*/
if(!head || !head->next)return head;
ListNode* res=head;
while(head->next){
if(head->val==head->next->val)head->next=head->next->next;
else head=head->next;
}
return res;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息