LeetCode 83. Remove Duplicates from Sorted List
Solution1:我的答案
思路和82题差不多,此算法还算是比较好的!
/**
* 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) {
struct ListNode* cur = head;
if (cur == NULL || cur->next == NULL)
return head;
while (cur->next) {
if (cur->val == cur->next->val)
cur->next = cur->next->next;
else
cur = cur->next;
}
return head;
}
};
本文介绍了解决LeetCode第83题“移除排序链表中的重复元素”的一种有效方法。通过一次遍历即可完成去重操作,适用于已排序的链表。文章提供了一个简洁的C++实现方案。
652

被折叠的 条评论
为什么被折叠?



