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

    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.



    非常easy。先推断第一个位置否为空。不为空的话再推断下 个位置是否为空,假设都为空那么就不用操作了。

    假设两个都不为空,那么就能够比較一下了。假设一样的话那就删除掉(指向下一个位置), 假设不一样,那么指针就指向下一个位置。


    /**
     * 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) {
            ListNode *p;
            p=head;
            while(p!=NULL)
            {
                if(p->next!=NULL)
                {
                    if(p->val==p->next->val)
                        p->next=p->next->next;
                    else
                        p=p->next;
                }
                else
                    return head;
            }
            return head;
        }
    };


  • 相关阅读:
    Nmap笔记
    Spring AOP(一)
    Spring IOC(三)
    Spring IOC(二)
    Spring IOC(一)
    bootstrap 使用(三)
    bootstrap 使用(二)
    bootstrap 使用(一)
    js(二)
    QQ邮件
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/6941331.html
Copyright © 2011-2022 走看看