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

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

    实现语言:Java

    /*
     public class ListNode {
        int val;
        ListNode next = null;
    
        ListNode(int val) {
            this.val = val;
        }
    }
    */
    public class Solution {
        public ListNode deleteDuplication(ListNode head){
            if(head==null){
                return null;
            }
            if(head!=null&&head.next==null){
                return head;
            }
            ListNode first=new ListNode(-1);
            first.next=head;
            ListNode last=first;
            ListNode p=head;
            while(p!=null&&p.next!=null){
                if(p.val==p.next.val){
                    int val=p.val;
                    while(p!=null&&p.val==val){
                        if(p.val==val){
                            p=p.next;
                        }
                    }
                    last.next=p;
                }else{
                    last=p;
                    p=p.next;
                }
            }
            return first.next;
        }
    }
    

     实现语言:Java

    /*
     public class ListNode {
        int val;
        ListNode next = null;
    
        ListNode(int val) {
            this.val = val;
        }
    }
    */
    public class Solution {
        public ListNode deleteDuplication(ListNode head){
            if(head==null){
                return null;
            }
            if(head!=null&&head.next==null){
                return head;
            }
            ListNode cur=null;
            if(head.val==head.next.val){
                cur=head.next.next;
                while(cur!=null&&cur.val==head.val){
                    cur=cur.next;
                }
                return deleteDuplication(cur);
            }else{
                cur=head.next;
                head.next=deleteDuplication(cur);
                return head;
            }
        }
    }
    
  • 相关阅读:
    Neditor 2.1.16 发布,修复缩放图片问题
    Neditor 2.1.16 发布,修复缩放图片问题
    每日 30 秒 ⏱ 强风吹拂
    每日 30 秒 ⏱ 强风吹拂
    Python3的日期和时间
    内置函数:exec、eval、compile
    内置函数:zip、filter、map、stored
    匿名函数
    列表推导式和生成器表达式
    生成器send的用法案例
  • 原文地址:https://www.cnblogs.com/xidian2014/p/10201613.html
Copyright © 2011-2022 走看看