博客
关于我
九章基础算法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/

    你可能感兴趣的文章
    Python 中的继承机制是什么样的?
    查看>>
    Python 中的装饰器是什么?
    查看>>
    Python 中的装饰器是如何工作的,有哪些实际应用场景?
    查看>>
    python读excel
    查看>>
    Python 中读取 CSV 文件-ChatGPT4o作答
    查看>>
    python请求html_使用Python请求获取HTML?
    查看>>
    Python 之匿名函数和偏函数
    查看>>
    python 之栈的实现
    查看>>
    python语音播放
    查看>>
    python语言:装饰器原理
    查看>>
    Python 交互式数据可视化详解
    查看>>
    python语言有哪些优点和缺点_Python有哪些优缺点,你了解吗?
    查看>>
    Python 从入门到精通:30天速成教程到底有多狠?你能坚持下来吗?
    查看>>
    Python 从数据库中存储和检索密码的最安全方法
    查看>>
    Python语言及其应用 - 知识点遍历
    查看>>
    Python 优化提速的 8 个小技巧
    查看>>
    Python 余弦相似度与皮尔逊相关系数 计算
    查看>>
    python 使用execjs 报编码错误解决办法,UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xac in position 145: il
    查看>>
    python 使用filetype校验文件
    查看>>
    Python 使用flush函数将缓冲区数据立即写磁盘
    查看>>