Given a sorted linked list, delete all duplicates such that each element appear only once.

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) {
        if(!head) return head;
        
        ListNode *previous = head;
        ListNode *current = head->next;
        while(current)
        {
            if(current->val == previous->val)
            {
                previous->next = current->next;
            }
            else
            {
                previous = previous->next;
            }
            
            current = current->next;
        }
        return head;
    }
};

 

相关文章:

  • 2021-06-29
  • 2022-01-19
  • 2021-06-02
  • 2022-01-05
  • 2021-05-17
  • 2021-05-31
  • 2021-11-05
  • 2021-05-27
猜你喜欢
  • 2021-10-16
  • 2022-12-23
相关资源
相似解决方案