首页 » 编程之美 » 正文

[leetcode_83]Remove Duplicates from Sorted List

去除链表中的重复数值。这个题 怎么说 现在发现,链表的题,最好存before, now , next这样的话 实现逻辑就很简单了~

class Solution {
public:
    ListNode *deleteDuplicates(ListNode *head) {
        // Note: The Solution object is instantiated only once and is reused by each test case.
        ListNode *headbackup = head;
        int val = 0;
        if(head == NULL)
        {
            return headbackup;
        }
        ListNode * before = head;
        ListNode * now = head->next;
        val = before->val;
        while(now != NULL)
        {
            if(val != now->val)
            {
                val = now->val;
                before = now;
                now = now->next;
            }
            else
            {
                now = now->next;
                before->next = now;
            }
        }
        return headbackup;
    }
};

发表评论