美文网首页
83. Remove Duplicates from Sorte

83. Remove Duplicates from Sorte

作者: YellowLayne | 来源:发表于2017-06-17 14:14 被阅读0次

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

    相关文章

      网友评论

          本文标题:83. Remove Duplicates from Sorte

          本文链接:https://www.haomeiwen.com/subject/fxzvqxtx.html