83_Remove Duplicates from Sorted List

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

For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.spa

有序的鏈表,去掉重複的節點code

注意:重複的可能不止一個blog

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* deleteDuplicates(struct ListNode* head) {
    struct ListNode * p = head;
    while(p != NULL && p->next != NULL)
    {
        while(p->next != NULL && p->val == p->next->val)
        {
            p->next = p->next->next;
        }
        p = p->next;
    }
    return head;
}
相關文章
相關標籤/搜索