• 删除链表中重复的结点


    题目描述:在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表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;
            }
        }
    }
    
  • 相关阅读:
    Mysql如何修改unique key
    centos 编译 安装 protobuf
    EasyNetQ简单使用
    微信发送模板消息
    Python删除开头空格
    代码积累-Common
    sql With(NoLock),With(ReadPast)
    webform 使用log4net配置
    log4net.dll添加报错
    js-小数计算问题
  • 原文地址:https://www.cnblogs.com/xidian2014/p/10201613.html
Copyright © 2020-2023  润新知