zoukankan      html  css  js  c++  java
  • LeetCode:Remove Duplicates from Sorted List I II

    LeetCode: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.

    分析:和从数组中移除重复元素那一题一样的思想

    /**
     * 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) {
            // IMPORTANT: Please reset any member data you declared, as
            // the same Solution instance will be reused for each test case.
            if(head == NULL || head->next == NULL)return head;
            ListNode *index = head, *p = head->next, *pre = head;
            while(p != NULL)
            {
                if(p->val != pre->val)
                {
                    index = index->next;
                    index->val = p->val;
                }
                pre = p;
                p = p->next;
            }
            p = index->next;
            index->next = NULL;
            while(p != NULL)
            {//销毁多余的节点
                ListNode *tmp = p;
                p = p->next;
                delete tmp;
            }
            return head;
        }
    };

    LeetCode: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.                                                  本文地址

    /**
     * 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) {
            // IMPORTANT: Please reset any member data you declared, as
            // the same Solution instance will be reused for each test case.
            if(head == NULL)return head;
            //为操作方便添加头结点
            ListNode *addHead = new ListNode(0);
            addHead->next = head;
            //index 指向已经保存的最后一个节点
            ListNode *index = addHead, *p = head;
            while(p != NULL)
            {
                if(p->next == NULL || p->val != p->next->val)
                {//当前节点没有重复
                    index = index->next;
                    index->val = p->val;
                    p = p->next;
                }
                else
                {//当前节点有重复,找到当前节点的最后一个副本的下一个元素
                    while(p->next && p->val == p->next->val)
                        p = p->next;
                    p = p->next;
                }
            }
            p = index->next;
            index->next = NULL;
            while(p != NULL)
            {//销毁多余的节点
                ListNode *tmp = p;
                p = p->next;
                delete tmp;
            }
            head = addHead->next;
            delete addHead;
            return head;
        }
    };

    【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3461481.html

  • 相关阅读:
    Web安全
    前端安全之XSS攻击
    SQL盲注
    Vim使用手册
    VC获取cookies的几种方法
    Wireshark基本介绍和学习TCP三次握手
    细说Cookie
    top100tools
    如何更改Jframe里Jpanel的大小
    HTTP&&Fiddler教程
  • 原文地址:https://www.cnblogs.com/TenosDoIt/p/3461481.html
Copyright © 2011-2022 走看看