美文网首页
2019-01-10 Day 5

2019-01-10 Day 5

作者: 骚得过火 | 来源:发表于2019-01-10 22:32 被阅读0次

    Day 5 01-10-2019
    来源:LeetCode
    给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。

    示例 1:

    输入: 1->1->2
    输出: 1->2
    示例 2:

    输入: 1->1->2->3->3
    输出: 1->2->3

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode* deleteDuplicates(ListNode* head) {
            
            if(head == NULL || head ->next == NULL)
                return head;
            
            ListNode * cur , *next ;
            cur = head ; next = head->next ;
            
            for( ; next != NULL ; next = next -> next )
            {
                if( cur -> val != next -> val) //如果两数不相等
                {
                    cur -> next = next;
                    cur = cur -> next;                
                }
            }
            cur->next = NULL;
            
            return head ;
        }
    };
    
    

    相关文章

      网友评论

          本文标题:2019-01-10 Day 5

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