zoukankan      html  css  js  c++  java
  • 删除链表中重复的结点

    题目描述

    在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5

    代码

    /*
    struct ListNode {
        int val;
        struct ListNode *next;
        ListNode(int x) :
            val(x), next(NULL) {
        }
    };
    */
    class Solution {
    public:
        ListNode* deleteDuplication(ListNode* pHead)
        {
    		if (pHead == NULL || pHead->next == NULL) {
                return pHead;
            }
            if (pHead->val != pHead->next->val) {//不相等,不需要删除
                pHead->next = deleteDuplication(pHead->next);
                return pHead;
            }
            
            pHead = pHead->next;
            while (pHead->next != NULL && pHead->val == pHead->next->val) {//删除重复结点
                pHead = pHead->next;
            }
            return deleteDuplication(pHead->next);
        }
    };
    
  • 相关阅读:
    RoIPooling、RoIAlign笔记
    ROI Align 的基本原理和实现细节
    ROI Align详解
    GIT总结
    java-变量,函数 下
    linux设置静态ip地址
    技术参考网站-网址
    python
    python
    python
  • 原文地址:https://www.cnblogs.com/jecyhw/p/6649610.html
Copyright © 2011-2022 走看看