代码改变世界

83_Remove Duplicates from Sorted List

2016-03-21 19:50  FTD_W  阅读(128)  评论(0编辑  收藏  举报

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

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

有序的链表,去掉重复的节点

注意:重复的可能不止一个

/**
 * 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;
}