梦想天地

导航

算法

83. Remove Duplicates from Sorted List

 

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.

Subscribe to see which companies asked this question

 

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* deleteDuplicates(struct ListNode* head) {
    struct ListNode *p,*p1;
    int val;
    p=head;
    while(p && p->next)
    {
        val=p->val;
        p1=p->next;
        
        if(p1->val == val)
        {
        p->next = p1->next;
        continue;
        }
        else
        {
        p=p1;
        continue;
        }
        
    }
    return head;
}

posted on 2016-05-18 11:23  梦想天地  阅读(146)  评论(0编辑  收藏  举报