zoukankan      html  css  js  c++  java
  • Remove Duplicates from Sorted List


    描述
    Given a sorted linked list, delete all duplicates such that each element appear only once.
    For example,
    Given 1->1->2, return 1->2.
    Given 1->1->2->3->3, return 1->2->3.

    删除重复节点

    代码

     1 public static ListNode deleteDuplicates(ListNode head) {
     2         if (head == null || head.next == null) {
     3             return head;
     4         }
     5         ListNode ptr1 = head;
     6         ListNode ptr2 = head.next;
     7         while (ptr2 != null) {
     8             if (ptr1.data == ptr2.data) {
     9                 ptr2 = ptr2.next;
    10                 if (ptr2 == null)
    11                     ptr1.next = null;
    12             } else {
    13                 ptr1.next = ptr2;// 衔接链表
    14                 ptr1 = ptr1.next;// 各移到下一个节点
    15                 ptr2 = ptr2.next;
    16             }
    17         }
    18         return head;
    19     }
  • 相关阅读:
    线程 详解
    登录时,添加图片验证码
    String、StringBuffer、StringBuilder详解
    Random 生成随机数
    down
    九九归一
    小R与手机
    coins
    noip注意事项
    小W计树
  • 原文地址:https://www.cnblogs.com/ncznx/p/9297111.html
Copyright © 2011-2022 走看看