zoukankan      html  css  js  c++  java
  • LeetCode OJ--Remove Duplicates from Sorted List II *

    http://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/

    处理链表的范例

    #include <iostream>
    using namespace std;
     
    struct ListNode {
         int val;
         ListNode *next;
         ListNode(int x) : val(x), next(NULL) {}
     };
     
    class Solution {
    public:
        ListNode *deleteDuplicates(ListNode *head) {
            if(head == NULL) return head;
    
            ListNode dummy(INT_MIN);
            dummy.next = head;
            ListNode *prev = &dummy,*cur = head;
            while(cur!= NULL)
            {
                bool duplicated = false;
                while(cur->next != NULL && cur->val == cur->next->val)
                {
                    duplicated = true;
                    cur = cur->next;
                }
                if(duplicated)
                {
                    cur = cur->next;
                    continue;
                }
                prev->next = cur;
                prev = prev->next;
                cur = cur->next;
            }
            prev->next = cur;
            return dummy.next;
        }
    };
    
    int main()
    {
        ListNode *n1 = new ListNode(1);
        ListNode *n2 = new ListNode(2);
        ListNode *n3 = new ListNode(3);
        ListNode *n4 = new ListNode(3);
        ListNode *n5 = new ListNode(3);
        ListNode *n6 = new ListNode(4);
        ListNode *n7 = new ListNode(4);
        n1->next = n2;
        n2->next = n3;
        n3->next = n4;
        n4->next = n5;
        n5->next = n6;
        n6->next = n7;
        ListNode *ans;
        Solution myS;
        ans = myS.deleteDuplicates(n1);
        return 0;
    }
  • 相关阅读:
    HDU-1225 Football Score
    HDU-3854 LOOPS
    HDU-3863 No Gambling
    poj-2096 Collecting Bugs
    HDU-4336 Card Collector
    HDU-4405 Aeroplane chess
    2010上交:最小面积子矩阵
    VijosP1443:银河英雄传说
    VijosP1250:分组背包
    Vijos1221:神秘的配方
  • 原文地址:https://www.cnblogs.com/qingcheng/p/3549077.html
Copyright © 2011-2022 走看看