Remove Duplicates from Sorted List

鏈表問題如果沒編過,查代碼一定有哪裏出現指針飛了,這個地方是null然而還操作......

/**
 * 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 *re=head;
        while(re)
        {
            while(re->next&&re->val==re->next->val)
            {
                re->next=re->next->next;
            }
            re=re->next;
        }
        return head;
    }
};


發佈了56 篇原創文章 · 獲贊 9 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章