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.這一題的邏輯非常簡單
重複的就跳過就是如此
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode* ans = head;
while(head != nullptr && head->next != nullptr)
{
if(head->val == head->next->val)
{
head->next = head->next->next;
}
else
{
head = head->next;
}
}
return ans;
}
};
沒有留言 :
張貼留言