Leetcode - List - Remove Duplicates from Sorted List
2014-08-13 20:42
323 查看
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given
Given
For example,
Given
1->1->2, return
1->2.
Given
1->1->2->3->3, return
1->2->3.
/** * 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) return NULL; ListNode *prev=head; ListNode *cur=head->next; while(cur!=NULL) { if(cur->val==prev->val) { prev->next=cur->next; delete cur; cur=prev->next; } else { prev=cur; cur=cur->next; } } return head; } };
相关文章推荐
- leetcode 38: Remove Duplicates from Sorted List II
- LeetCode - Remove Duplicates from Sorted List
- leetcode 37: Remove Duplicates from Sorted List
- LeetCode_Remove Duplicates from Sorted List II
- LeetCode Online Judge 题目C# 练习 - Remove Duplicates from Sorted List II
- LeetCode: Remove Duplicates from Sorted List
- [leetcode] Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List
- Leetcode: Remove Duplicates from Sorted List
- leetcode 38: Remove Duplicates from Sorted List II
- [LeetCode] Remove Duplicates from Sorted List
- [LeetCode] 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, Solution
- [LeetCode] Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List II
- LeetCode_Remove Duplicates from Sorted List
- [leetcode] Remove Duplicates from Sorted List