zoukankan      html  css  js  c++  java
  • 链表介绍

    单向链表

    单向链表节点结构

    public class Node {
       public int value;
       public Node next;
       public Node(int data) {
           value = data;
      }
    }

    双向链表

    双向链表节点结构
    public class DoubleNode {
       public int value;
       public DoubleNode last;
       public DoubleNode next;

       public DoubleNode(int data) {
           value = data;
      }
    }

    单链表和双链表如何反转

        /**
        * 单链表反转
        * @param head
        * @return
        */
       private static Node reverseNodeList(Node head) {
           Node pre=null;
           Node next=null;
           while(head!=null){
               next=head.next;
               head.next=pre;
               pre=head;
               head=next;
          }
           return pre;
      }
       /**
        * 双链表反转
        * @param head
        * @return
        */
       public static DoubleNode reverseDoubleNode(DoubleNode head){
           DoubleNode pre=null;
           DoubleNode next=null;
           while(head!=null){
               next=head.next;
               head.next=pre;
               head.last=next;
               pre=head;
               head=next;
          }
           return pre;
      }

    把给定值都删除

        /**
        * 删除链表中的某一个值
        * @param head
        * @param num
        * @return
        */
       public static Node deleteNode(Node head,int num){
           while (head!=null){
               if (head.value!=num){   //头结点是否需要删除
                   break;
              }
               head=head.next;
          }
           Node pre=head;
           Node next=head;
           while(next!=null){
               if (next.value==num){
                   pre.next=next.next;
              }else{
                   pre=next;
              }
               next=next.next;
          }
           return head;
      }



  • 相关阅读:
    httpd服务器的真实ip获取难题
    nginx配置文件详解
    nginx基础知识总结
    Web服务并发I/O模型
    chrony时间服务器
    Linux运维之每日小技巧-检测网站状态以及PV、UV等介绍
    Centos7系统下编写systemd脚本设置redis开机自启动
    Kibana中的Coordinate Map地图报索引错误的问题
    apache的php模块讲解以及搭建phpmyadmin管理数据库mysql
    AMP架构补充与wordpress部署
  • 原文地址:https://www.cnblogs.com/wangyang1991/p/15188270.html
Copyright © 2011-2022 走看看