[Leetcode]Remove Duplicates from Sorted List

时间:2021-06-22 07:35:46

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

简单题,注意逻辑顺序就好了。

 1 class Solution {
 2 public:
 3     ListNode *deleteDuplicates(ListNode *head) {
 4         ListNode *node = head;
 5         while(node!=NULL){
 6             if(node->next!=NULL && node->val==node->next->val)
 7                 node->next=node->next->next;
 8             else
 9                 node = node->next;
10         }
11         return head;
12     }
13 };