zoukankan      html  css  js  c++  java
  • 83. Remove Duplicates from Sorted List

    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.

     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 == NULL){
    13             return head;
    14         }
    15         
    16         
    17         ListNode* l1 = head;
    18         
    19         while(l1 && l1->next)
    20         {
    21             if(l1->val != l1->next->val){
    22                 l1 = l1->next;
    23             }else{
    24                 ListNode* p = l1->next->next;
    25                 l1->next = p;
    26             }
    27             
    28         }
    29         return head;
    30     }
    31 };
  • 相关阅读:
    Mac OS X开发学习 -打开文件选择器并获取文件
    Mac OS X开发学习
    第三方开源
    Xcode操作
    Mac操作
    XCode6.1 错误
    ios 本地推送
    IOS 语法
    IOS 其他
    IOS 语法
  • 原文地址:https://www.cnblogs.com/sankexin/p/5864319.html
Copyright © 2011-2022 走看看