博客
关于我
九章基础算法01:链表
阅读量:261 次
发布时间:2019-03-01

本文共 2639 字,大约阅读时间需要 8 分钟。

????

????

??????????????????????????????????????????????????????????????????????????????????????

????

  • ????

    ???????????Node???????????????

    • ????Data???????????????
    • ???Pointer??????????null?????????????
  • ??????

    ????????????????????????????????????????????????????????????


  • Dummy????

    ?????????????dummy?????????

    Dummy?????

  • ??????

    ??dummy???????????????????????????????????

  • ?????

    • ?????dummy?????????????????????????
    • ??????????dummy????????
  • ??????

    • ???????dummy???next??null???????????

  • ??????

    ??????

    class ListNode {    public int val;    public ListNode next;    public ListNode(int value) {        val = value;    }}
    • ?????
      ??????next?????null?

    ????

    public class LinkedList {    private ListNode dummy;    public LinkedList() {        dummy = new ListNode(-1);    }}
    • ????
      ?????dummy??????????????????

    ??????

    1. ????

    public void add(int location, int val) {    ListNode pre = dummy;    for (int i = 0; i < location; i++) {        pre = pre.next;    }    ListNode node = new ListNode(val);    node.next = pre.next;    pre.next = node;}
    • ????
      ???0?????????????????????

    2. ????

    public void remove(int location) {    ListNode pre = dummy;    for (int i = 0; i < location; i++) {        pre = pre.next;    }    pre.next = pre.next.next;}
    • ????
      ???????????????next????????

    3. ????

    public int get(int location) {    ListNode cur = dummy.next;    for (int i = 0; i < location; i++) {        cur = cur.next;    }    return cur.val;}
    • ????
      ?????????????????

    4. ????

    public boolean contain(int val) {    ListNode cur = dummy.next;    while (cur != null) {        if (cur.val == val) {            return true;        }        cur = cur.next;    }    return false;}
    • ????
      ?dummy??????????????????????????

    5. ????

    public void print() {    ListNode cur = dummy.next;    while (cur != null) {        System.out.print(cur.val + "->");        cur = cur.next;    }    System.out.println("null");}
    • ????
      ????????????null????????

    ????

    public static void main(String[] args) {    LinkedList list = new LinkedList();    list.add(0, 10);    list.add(1, 20);    list.add(2, 40);    list.remove(1);    list.print();    System.out.println(list.get(0));    System.out.println(list.get(1));    System.out.println(list.contain(50));    System.out.println(list.contain(40));}
    • ????
      ?????????????????????

    ArrayList?LinkedList???????

    ???? ArrayList LinkedList ?????
    ???add? O(n) O(n) ????????????
    ???remove? O(n) O(n) ??????
    ???get? O(1) O(n) ArrayList????
    ???contain? O(n) O(n) ???????

    LeetCode????

    ????

    • Solution???

      LeetCode????Solution??????????????????

    • ??????

      ??main???????????????????

    ????

  • ????????aplusb??
  • ???????????
  • ???????????

  • ??

    ?????????????????????????????????????????????????

    转载地址:http://ahwx.baihongyu.com/

    你可能感兴趣的文章
    numpy数组替换其中的值(如1替换为255)
    查看>>
    numpy数组索引-ChatGPT4o作答
    查看>>
    numpy最大值和最大值索引
    查看>>
    NUMPY矢量化np.prod不能构造具有超过32个操作数的ufunc
    查看>>
    Numpy矩阵与通用函数
    查看>>
    numpy绘制热力图
    查看>>
    numpy转PIL 报错TypeError: Cannot handle this data type
    查看>>
    Numpy闯关100题,我闯了95关,你呢?
    查看>>
    nump模块
    查看>>
    Nutch + solr 这个配合不错哦
    查看>>
    NuttX 构建系统
    查看>>
    NutUI:京东风格的轻量级 Vue 组件库
    查看>>
    NutzCodeInsight 2.0.7 发布,为 nutz-sqltpl 提供友好的 ide 支持
    查看>>
    NutzWk 5.1.5 发布,Java 微服务分布式开发框架
    查看>>
    NUUO网络视频录像机 css_parser.php 任意文件读取漏洞复现
    查看>>
    NUUO网络视频录像机 upload.php 任意文件上传漏洞复现
    查看>>
    Nuxt Time 使用指南
    查看>>
    NuxtJS 接口转发详解:Nitro 的用法与注意事项
    查看>>
    NVDIMM原理与应用之四:基于pstore 和 ramoops保存Kernel panic日志
    查看>>
    NVelocity标签使用详解
    查看>>