83. Remove Duplicates from Sorte

2017-06-17  本文已影响0人  YellowLayne

1.描述

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.

2.分析

3.代码

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* deleteDuplicates(struct ListNode* head) {
    if (NULL == head || NULL == head->next) return head;
    
    struct ListNode* cur = head;
    while (NULL != cur->next) {
        if (cur->val == cur->next->val) {
            struct ListNode* tmp = cur->next;
            cur->next = tmp->next;
            free(tmp);
        } else {
            cur = cur->next;
        }
    }
    return head;
}
上一篇下一篇

猜你喜欢

热点阅读