zoukankan      html  css  js  c++  java
  • 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) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            if(head == NULL || head->next == NULL) return head;
            int curVal = head->val;
            // P指向已经处理的合法listNode 的最后一个Node
            //q 指向当前政要处理的节点
            ListNode *p = NULL,*q = head, *temp;
            head = NULL ;       
            bool needDelete = false ;
            while(q)
            {  
                if(needDelete == false){
                    if(q->next && q->next->val == q->val){
                         curVal = q->val;
                         needDelete = true;
                         temp = q->next->next;
                         delete q->next;
                         delete q;
                         q = temp;
                    }else{
                         if(head == NULL)
                         { 
                            head = q;
                            p = q;
                         }else{
                          p->next = q;
                          p = q;
                          }
                          q = q->next;
                    }                
                }else{ // else of needDelete = true
                
                     if(q->val == curVal)
                     {
                        temp = q->next;
                        delete q;
                        q = temp;
                     }else{
                         needDelete =false;
                     }
                }
            }
            
            if(p)
                p->next = NULL;
            
            return head ;
        }
    };

     重写: 加了个虚拟根节点,从然使得自己从边界中解放出来

    /**
     * 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) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            if(head == NULL || head ->next == NULL) return head;
            
    		ListNode * head0, *cur;
    		head0 = new ListNode(0);
    		head0->next = head;
    		head = head0;
    		cur = head0;
    		
    		while(cur->next != NULL){
    			ListNode * p = cur->next;
    			ListNode *temp;
    			while(p->next != NULL && p->next->val == p->val)
    			{
        			temp = p;
    			    p = p->next;
    			    delete temp;
    			}
    			if(p == cur->next){
    				cur = cur->next;
    			}else{
    				cur->next = p->next; 
    				delete p;
    			}
    		}
    		
    		
    		return head->next;
        }
    };
    

      

    --------------------------------------------------------------------天道酬勤!
  • 相关阅读:
    [更新设计]跨平台物联网通讯框架ServerSuperIO 2.0 ,功能、BUG、细节说明,以及升级思考过程!
    有幸参加“集团2016年工业事业部发展规划会议”,向网友汇报!!!
    Centos7之ssh连接keepalive
    AES加密算法
    MySQL之only_full_group_by
    AES加解密文件流
    Mongo基本配置
    前端js处理接口返回的压缩包(亲测可用)
    Ubuntu20.4静态ip和dhcp配置
    Java数组类型协变性、泛型类型的不变性
  • 原文地址:https://www.cnblogs.com/graph/p/3048338.html
Copyright © 2011-2022 走看看