zoukankan      html  css  js  c++  java
  • 剑指Offer(Java版)第六十一题:在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点, 重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5

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

    public class ListNode{
    int val;
    ListNode next = null;
    ListNode(int val){
    this.val = val;
    }
    }

    public ListNode deleteDuplication(ListNode head){
    if(head == null || head.next == null){
    return head;
    }
    ListNode first = new ListNode(0);
    first.next = head;
    ListNode pre = first;
    ListNode current = head;
    while(current != null){
    if(current.next != null && current.val == current.next.val){
    while(current.next != null && current.val == current.next.val){
    current = current.next;
    }
    pre.next = current.next;
    current = current.next;
    }else{
    pre = pre.next;
    current = current.next;
    }
    }
    return first.next;
    }

    public static void main(String[] args) {
    // TODO Auto-generated method stub

    }

    }

  • 相关阅读:
    mongo相关
    grafana相关
    问题与解决
    蓝鲸社区版6.0填坑指南
    go环境
    docker相关
    gitlab相关
    LRU(Least recently used,最近最少使用)
    LRU:最近最久未使用
    学习大神笔记之 “MyBatis学习总结(一)”
  • 原文地址:https://www.cnblogs.com/zhuozige/p/12548839.html
Copyright © 2011-2022 走看看