zoukankan      html  css  js  c++  java
  • leetcode82 Remove Duplicates from Sorted List II

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

    For example,
    Given 1->2->3->3->4->4->5, return 1->2->5.
    Given 1->1->1->2->3, return 2->3.

     1 /**
     2  * Definition for singly-linked list.
     3  * struct ListNode {
     4  *     int val;
     5  *     ListNode *next;
     6  *     ListNode(int x) : val(x), next(NULL) {}
     7  * };
     8  */
     9 class Solution {
    10 public:
    11     ListNode* deleteDuplicates(ListNode* head) {
    12         if(!head)
    13             return NULL;
    14         ListNode *ans=new ListNode(0);
    15         ListNode *p=ans;
    16         int cur=head->val;
    17         int count=1;
    18         head=head->next;
    19         while(head)
    20         {
    21             if(head->val!=cur)
    22             {
    23                 if(count==1)
    24                 {
    25                     ListNode *temp=new ListNode(cur);
    26                     p->next=temp;
    27                     p=p->next;
    28                 }
    29                 cur=head->val;
    30                 count=1;
    31             }
    32             else
    33             {
    34                 count++;
    35             }
    36             head=head->next;
    37         }
    38                 if(count==1)
    39                 {
    40                     ListNode *temp=new ListNode(cur);
    41                     p->next=temp;
    42                     p=p->next;
    43                 }
    44         return ans->next;
    45     }
    46 };
    View Code
  • 相关阅读:
    BZOJ3589: 动态树
    BZOJ3631: [JLOI2014]松鼠的新家
    BZOJ3307: 雨天的尾巴
    BZOJ1895: Pku3580 supermemo
    BZOJ3786: 星系探索
    BZOJ2819: Nim
    解题:POI 2009 Lyz
    解题:POI 2016 Nim z utrudnieniem
    解题:POI 2004 Bridge
    解题:POI 2018 Prawnicy
  • 原文地址:https://www.cnblogs.com/jsir2016bky/p/5105981.html
Copyright © 2011-2022 走看看