83.Remove Duplicates from Sorted List
程序员文章站
2022-03-15 20:35:59
...
题目:
Given a sorted linked list, delete all duplicates such that each element appear only once.
Example 1:
Input: 1->1->2
Output: 1->2
Example 2:
Input: 1->1->2->3->3
Output: 1->2->3
解析:对已经排序的链表中,删除重复的元素,只留一个。
方法:
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode *h = head;
// ListNode *p = h->next;
while(h && h->next)
{
if(h->val == h->next->val)
{
h->next = h->next->next;
// p=h->next;
}
else
{
h=h->next;
// p=h->next;
}
}
return head;
}
};
待解决问题:在while中的条件,调换位置或者少其中一个后会导致超时;同时把h->next显示赋值给另一变量也会超时????
推荐阅读
-
leetcode82: Remove Duplicates from Sorted ListII
-
【一天一道LeetCode】#26. Remove Duplicates from Sorted Array
-
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
-
Leetcode No.26 Remove Duplicates from Sorted Array(c++实现)
-
26. Remove Duplicates from Sorted Array
-
【LeetCode】80. Remove Duplicates from Sorted Array II (删除排序数组中的重复项 II)-C++实现及详细图解