zoukankan      html  css  js  c++  java
  • 【链表】有序链表中移除重复项

     1 public class Main {
     2 
     3     public Node removeDup(Node node){
     4 
     5         if (node == null || node.next == null || node.next.next == null){
     6             return node;
     7         }
     8 
     9         Node pre = node.next;
    10         Node cur = node.next.next;
    11 
    12         while (cur != null){
    13             if (cur.data == pre.data){
    14                 pre.next = cur.next;
    15             }else {
    16                 pre = cur;
    17             }
    18             cur = cur.next;
    19         }
    20 
    21         return node;
    22     }
    23 
    24 
    25     public Node createListNodes() {
    26         Node node7 = new Node(7, null);
    27         Node node6 = new Node(5, node7);
    28         Node node5 = new Node(5, node6);
    29         Node node4 = new Node(5, node5);
    30         Node node3 = new Node(3, node4);
    31         Node node2 = new Node(3, node3);
    32         Node node1 = new Node(1, node2);
    33         Node head = new Node(0, node1); // head pointer
    34 
    35         return head;
    36     }
    37 
    38     public static void main(String[] args) {
    39         Main main = new Main();
    40         Node node = main.removeDup(main.createListNodes());
    41 
    42         if (node != null) {
    43             node = node.next;
    44             while (node != null) {
    45                 System.out.println(node.data);
    46                 node = node.next;
    47             }
    48         }
    49     }
    50 }
  • 相关阅读:
    Eclipse添加注释简介
    git config配置文件
    ndk开发教程以及问题解决方案
    PATH路径出错导致任何命令都找不到解决方法
    git详细教程
    ssh相关操作
    ORM SQLAlchemy 表于表的关系
    SQLAlchemy 使用
    ORM SQLAlchemy 简介
    LUA基础
  • 原文地址:https://www.cnblogs.com/jiangyi-uestc/p/10052440.html
Copyright © 2011-2022 走看看