【leetcode】83-Remove Duplicates from Sorted List

problem

Remove Duplicates from Sorted List

考察的是链表的基本用法。

code

/**
 * 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) {
        ListNode* cur = head;
        while(cur && cur->next)//null is error.
        {
            if(cur->val == cur->next->val) cur->next = cur->next->next;
            else cur = cur->next;   //        
        }
        return head;
    }
};

 

 

参考

1. Remove Duplicates from Sorted List;

posted on 2018-11-25 16:36  鹅要长大  阅读(155)  评论(0编辑  收藏  举报

导航