码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode-5-Longest Palindromic Characters

时间:2019-02-01 14:12:17      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:col   dup   longest   inpu   output   numbers   基本   The   out   

算法描述:

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

Example 1:

Input: 1->2->3->3->4->4->5
Output: 1->2->5

Example 2:

Input: 1->1->1->2->3
Output: 2->3

解题思路:列表题,画出图基本上就能解出来。

    ListNode* deleteDuplicates(ListNode* head) {
        ListNode* dummy = new ListNode(-1);
        dummy->next =head;
        ListNode* cur = dummy;
        while(cur->next!=nullptr && cur->next->next!=nullptr){
            if(cur->next->val == cur->next->next->val){
                int data = cur->next->val;
                while(cur->next!=nullptr && cur->next->val == data){
                    cur->next = cur->next->next;
                }
            }else{
                cur = cur->next;
            }
        }
        return dummy->next;
    }

 

LeetCode-5-Longest Palindromic Characters

标签:col   dup   longest   inpu   output   numbers   基本   The   out   

原文地址:https://www.cnblogs.com/nobodywang/p/10345460.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!