链表-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;
}
};
分析:因为链表已经是有序的,所以重复的值都会集中在一起,所以直接遍历,删除重复值就行。
题解:
/**
* 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;
}
};
相关文章推荐
- Leetcode 83 Remove Duplicates from Sorted List 有序链表去重
- 【leetcode】【单链表】【83】Remove Duplicates from Sorted List
- LeetCode-83-Remove Duplicates from Sorted List(链表)-Medium
- LeetCode 83 Remove Duplicates from Sorted List (链表)
- LeetCode 83 Remove Duplicates from Sorted List(从已排序链表中移除重复元素)(*)
- 链表-python-leetcode 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(链表操作)
- 【leetcode】【单链表】【83】Remove Duplicates from Sorted List
- [leetcode 83]Remove Duplicates from Sorted List
- LeetCode | Remove Duplicates from Sorted List(删除链表中重复的数据)
- LeetCode-Remove Duplicates from Sorted List II-从有序链表中移除重复元素-链表操作
- [LeetCode] Remove Duplicates from Sorted List II 链表
- leetcode_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 II
- [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项
- [leetcode]83 Remove Duplicates from Sorted List