【算法之旅】基础数据结构之链表(一)

简介: 【算法之旅】基础数据结构之链表(一)

一、概述


定义


在计算机科学中,链表是数据元素的线性集合,其每个元素都指向下一个元素,元素存储上并不连续


In computer science, a linked list is a linear collection of data elements whose order is not given by their physical placement in memory. Instead, each element points to the next.


可以分类为


单向链表,每个元素只知道其下一个元素是谁


dbd4f8a2705770bc8b81bff14b92cb2b_3173af5e66d56d59dd889a20b79b7fbe.png


双向链表,每个元素知道其上一个元素和下一个元素


ae8b3308000ca3c9da68722524a12359_0096ba478c631ba2adc080f9bbae6948.png


循环链表,通常的链表尾节点 tail 指向的都是 null,而循环链表的 tail 指向的是头节点 head


2533de4298344697cbffc0b07146cd7b_4371c3320cf4e2b654c997f88b5274f4.png


链表内还有一种特殊的节点称为哨兵(Sentinel)节点,也叫做哑元( Dummy)节点,它不存储数据,通常用作头尾,用来简化边界判断,如下图所示


ca3aa59a597b30a4e9da2601ef0d15f4_468502cf4ce00a2f8b91bbf682578da9.png


随机访问性能


根据 index 查找,时间复杂度 O ( n ) O(n)O(n)


插入或删除性能


起始位置:O ( 1 ) O(1)O(1)

结束位置:如果已知 tail 尾节点是 O ( 1 ) O(1)O(1),不知道 tail 尾节点是 O ( n ) O(n)O(n)

中间位置:根据 index 查找时间 + O ( 1 ) O(1)O(1)


二、单向链表


根据单向链表的定义,首先定义一个存储 value 和 next 指针的类 Node,和一个描述头部节点的引用


public class SinglyLinkedList {
    private Node head; // 头部节点
    private static class Node { // 节点类
        int value;
        Node next;
        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }
    }
}


Node 定义为内部类,是为了对外隐藏实现细节,没必要让类的使用者关心 Node 结构

定义为 static 内部类,是因为 Node 不需要与 SinglyLinkedList 实例相关,多个 SinglyLinkedList实例能共用 Node 类定义

头部添加


public class SinglyLinkedList {
    // ...
    public void addFirst(int value) {
  this.head = new Node(value, this.head);
    }
}


如果 this.head == null,新增节点指向 null,并作为新的 this.head

如果 this.head != null,新增节点指向原来的 this.head,并作为新的 this.head

注意赋值操作执行顺序是从右到左

while 遍历


public class SinglyLinkedList {
    // ...
    public void loop() {
        Node curr = this.head;
        while (curr != null) {
            // 做一些事
            curr = curr.next;
        }
    }
}


for 遍历


public class SinglyLinkedList {
    // ...
    public void loop() {
        for (Node curr = this.head; curr != null; curr = curr.next) {
            // 做一些事
        }
    }
}


以上两种遍历都可以把要做的事以 Consumer 函数的方式传递进来

Consumer 的规则是一个参数,无返回值,因此像 System.out::println 方法等都是 Consumer

调用 Consumer 时,将当前节点 curr.value 作为参数传递给它

迭代器遍历


public class SinglyLinkedList implements Iterable<Integer> {
    // ...
    private class NodeIterator implements Iterator<Integer> {
        Node curr = head;
        public boolean hasNext() {
            return curr != null;
        }
        public Integer next() {
            int value = curr.value;
            curr = curr.next;
            return value;
        }
    }
    public Iterator<Integer> iterator() {
        return new NodeIterator();
    }
}



hasNext 用来判断是否还有必要调用 next

next 做两件事

返回当前节点的 value

指向下一个节点

NodeIterator 要定义为非 static 内部类,是因为它与 SinglyLinkedList 实例相关,是对某个 SinglyLinkedList 实例的迭代

递归遍历


public class SinglyLinkedList implements Iterable<Integer> {
    // ...
    public void loop() {
        recursion(this.head);
    }
    private void recursion(Node curr) {
        if (curr == null) {
            return;
        }
        // 前面做些事
        recursion(curr.next);
        // 后面做些事
    }
}


尾部添加


public class SinglyLinkedList {
    // ...
    private Node findLast() {
        if (this.head == null) {
            return null;
        }
        Node curr;
        for (curr = this.head; curr.next != null; ) {
            curr = curr.next;
        }
        return curr;
    }
    public void addLast(int value) {
        Node last = findLast();
        if (last == null) {
            addFirst(value);
            return;
        }
        last.next = new Node(value, null);
    }
}



注意,找最后一个节点,终止条件是 curr.next == null

分成两个方法是为了代码清晰,而且 findLast() 之后还能复用

尾部添加多个


public class SinglyLinkedList {
    // ...
  public void addLast(int first, int... rest) {
        Node sublist = new Node(first, null);
        Node curr = sublist;
        for (int value : rest) {
            curr.next = new Node(value, null);
            curr = curr.next;
        }
        Node last = findLast();
        if (last == null) {
            this.head = sublist;
            return;
        }
        last.next = sublist;
    }
}



先串成一串 sublist

再作为一个整体添加

根据索引获取


public class SinglyLinkedList {
    // ...
  private Node findNode(int index) {
        int i = 0;
        for (Node curr = this.head; curr != null; curr = curr.next, i++) {
            if (index == i) {
                return curr;
            }
        }
        return null;
    }
    private IllegalArgumentException illegalIndex(int index) {
        return new IllegalArgumentException(String.format("index [%d] 不合法%n", index));
    }
    public int get(int index) {
        Node node = findNode(index);
        if (node != null) {
            return node.value;
        }
        throw illegalIndex(index);
    }
}



同样,分方法可以实现复用

插入


public class SinglyLinkedList {
    // ...
  public void insert(int index, int value) {
        if (index == 0) {
            addFirst(value);
            return;
        }
        Node prev = findNode(index - 1); // 找到上一个节点
        if (prev == null) { // 找不到
            throw illegalIndex(index);
        }
        prev.next = new Node(value, prev.next);
    }
}


插入包括下面的删除,都必须找到上一个节点

删除


public class SinglyLinkedList {
    // ...
  public void remove(int index) {
        if (index == 0) {
            if (this.head != null) {
                this.head = this.head.next;
                return;
            } else {
                throw illegalIndex(index);
            }
        }
        Node prev = findNode(index - 1);
        Node curr;
        if (prev != null && (curr = prev.next) != null) {
            prev.next = curr.next;
        } else {
            throw illegalIndex(index);
        }
    }
}



第一个 if 块对应着 removeFirst 情况

最后一个 if 块对应着至少得两个节点的情况

不仅仅判断上一个节点非空,还要保证当前节点非空

相关文章
|
11天前
|
存储
数据结构第二课 -----线性表之单向链表
数据结构第二课 -----线性表之单向链表
|
2天前
|
机器学习/深度学习 存储 算法
数据结构与算法 动态规划(启发式搜索、遗传算法、强化学习待完善)
数据结构与算法 动态规划(启发式搜索、遗传算法、强化学习待完善)
8 1
|
2天前
|
存储 算法 Java
数据结构与算法 数组和链表
数据结构与算法 数组和链表
7 0
|
2天前
|
存储 Java
深入浅出数据结构之链表
深入浅出数据结构之链表
|
2天前
|
C++
数据结构(双链表
数据结构(双链表
7 1
|
5天前
|
存储 缓存
[数据结构]~双向+循环链表从(0~1)
[数据结构]~双向+循环链表从(0~1)
|
5天前
|
搜索推荐 C语言
【C语言/数据结构】排序(归并排序|计数排序|排序算法复杂度)
【C语言/数据结构】排序(归并排序|计数排序|排序算法复杂度)
11 0
|
5天前
|
存储 算法
Leetcode 30天高效刷数据结构和算法 Day1 两数之和 —— 无序数组
给定一个无序整数数组和目标值,找出数组中和为目标值的两个数的下标。要求不重复且可按任意顺序返回。示例:输入nums = [2,7,11,15], target = 9,输出[0,1]。暴力解法时间复杂度O(n²),优化解法利用哈希表实现,时间复杂度O(n)。
16 0
|
11天前
|
存储
数据结构第三课 -----线性表之双向链表
数据结构第三课 -----线性表之双向链表
|
12天前
|
存储 机器学习/深度学习 算法