第4章 最基础的动态数据结构:链表
- 4-1 什么是链表
- 4-2 链表Linked List
- 4-3 使用链表的虚拟头结点
- 4-4 链表的遍历,查询和修改
- 4-5 从链表中删除元素
- 4-6 使用链表实现栈
- 4-7 带有尾指针的链表:使用链表实现队列
- 4-8 LeetCode中和链表相关的问题
- 4-9 测试自己的LeetCode链表代码
- 4-10递归基础与递归的宏观语意
- 4-11 链表的天然递归结构性质
- 4-12 递归运行的机制:递归的微观解读
4-1 什么是链表

为什么链表很重要
- 链表:真正的动态数据结构
- 最简单的动态数据结构
- 更深入的理解引用(或者指针)
- 更深入的理解递归
- 辅助组成其他数据结构
链表
数据存储在"节点(Node)中
class Node {
E e;
Node next;
}

优点:真正的动态,不需要处理固定容量的问题
缺点:丧失了随机访问的能力
数组与链表的对比
数组 |
链表 |
数组最好用于索引有语意的情况。scores[2] |
链表不适合索引有语意的情况 |
支持快速查询 |
动态 |
public class LinkedList<E> {
private class Node{
public E e;
public Node next;
public Node(E e, Node next){
this.e = e;
this.next = next;
}
public Node(E e){
this(e, null);
}
public Node(){
this(null, null);
}
}
4-2 链表Linked List
在链表头添加元素

public void addFirst(E e){
head = new Node(e, head);
size ++;
}
在链表中间和末尾添加元素


public void add(int index, E e){
if(index < 0 || index > size)
throw new IllegalArgumentException("Add failed. Illegal index.");
if(index == 0)
addFirst(e);
else{
Node prev = head;
for(int i = 0 ; i < index - 1 ; i ++)
prev = prev.next;
prev.next = new Node(e, prev.next);
size ++;
}
}
public void addLast(E e){
add(size, e);
}
4-3 使用链表的虚拟头结点

public class LinkedList<E> {
private class Node{
public E e;
public Node next;
public Node(E e, Node next){
this.e = e;
this.next = next;
}
public Node(E e){
this(e, null);
}
public Node(){
this(null, null);
}
@Override
public String toString(){
return e.toString();
}
}
private Node dummyHead;
private int size;
public LinkedList(){
dummyHead = new Node();
size = 0;
}
public int getSize(){
return size;
}
public boolean isEmpty(){
return size == 0;
}
public void add(int index, E e){
if(index < 0 || index > size)
throw new IllegalArgumentException("Add failed. Illegal index.");
Node prev = dummyHead;
for(int i = 0 ; i < index ; i ++)
prev = prev.next;
prev.next = new Node(e, prev.next);
size ++;
}
public void addFirst(E e){
add(0, e);
}
public void addLast(E e){
add(size, e);
}
}
4-4 链表的遍历,查询和修改
public E get(int index){
if(index < 0 || index >= size)
throw new IllegalArgumentException("Get failed. Illegal index.");
Node cur = dummyHead.next;
for(int i = 0 ; i < index ; i ++)
cur = cur.next;
return cur.e;
}
public E getFirst(){
return get(0);
}
public E getLast(){
return get(size - 1);
}
public void set(int index, E e){
if(index < 0 || index >= size)
throw new IllegalArgumentException("Set failed. Illegal index.");
Node cur = dummyHead.next;
for(int i = 0 ; i < index ; i ++)
cur = cur.next;
cur.e = e;
}
public boolean contains(E e){
Node cur = dummyHead.next;
while(cur != null){
if(cur.e.equals(e))
return true;
cur = cur.next;
}
return false;
}
4-5 从链表中删除元素

public E remove(int index){
if(index < 0 || index >= size)
throw new IllegalArgumentException("Remove failed. Index is illegal.");
Node prev = dummyHead;
for(int i = 0 ; i < index ; i ++)
prev = prev.next;
Node retNode = prev.next;
prev.next = retNode.next;
retNode.next = null;
size --;
return retNode.e;
}
public E removeFirst(){
return remove(0);
}
public E removeLast(){
return remove(size - 1);
}
public void removeElement(E e){
Node prev = dummyHead;
while(prev.next != null){
if(prev.next.e.equals(e))
break;
prev = prev.next;
}
if(prev.next != null){
Node delNode = prev.next;
prev.next = delNode.next;
delNode.next = null;
size --;
}
}
链表的时间复杂度分析
添加操作 |
时间复杂度 |
addLast(e) |
O(n) |
addFirst(e) |
O(1) |
add(index,e) |
O(n/2)=O(n) |
综合 |
O(n) |
删除操作 |
时间复杂度 |
removeLast(e) |
O(n) |
removeFirst(e) |
O(1) |
remove(index,e) |
O(n/2)=O(n) |
综合 |
O(n) |
修改操作 |
时间复杂度 |
set(index,e) |
O(n) |
查找操作 |
时间复杂度 |
get(index) |
O(n) |
contains(e) |
O(n) |

4-6 使用链表实现栈
public class LinkedListStack<E> implements Stack<E> {
private LinkedList<E> list;
public LinkedListStack(){
list = new LinkedList<>();
}
@Override
public int getSize(){
return list.getSize();
}
@Override
public boolean isEmpty(){
return list.isEmpty();
}
@Override
public void push(E e){
list.addFirst(e);
}
@Override
public E pop(){
return list.removeFirst();
}
@Override
public E peek(){
return list.getFirst();
}
4-7 带有尾指针的链表:使用链表实现队列
public class LinkedListQueue<E> implements Queue<E> {
private class Node{
public E e;
public Node next;
public Node(E e, Node next){
this.e = e;
this.next = next;
}
public Node(E e){
this(e, null);
}
public Node(){
this(null, null);
}
@Override
public String toString(){
return e.toString();
}
}
private Node head, tail;
private int size;
public LinkedListQueue(){
head = null;
tail = null;
size = 0;
}
@Override
public int getSize(){
return size;
}
@Override
public boolean isEmpty(){
return size == 0;
}
@Override
public void enqueue(E e){
if(tail == null){
tail = new Node(e);
head = tail;
}
else{
tail.next = new Node(e);
tail = tail.next;
}
size ++;
}
@Override
public E dequeue(){
if(isEmpty())
throw new IllegalArgumentException("Cannot dequeue from an empty queue.");
Node retNode = head;
head = head.next;
retNode.next = null;
if(head == null)
tail = null;
size --;
return retNode.e;
}
@Override
public E getFront(){
if(isEmpty())
throw new IllegalArgumentException("Queue is empty.");
return head.e;
}
}
4-8 LeetCode中和链表相关的问题
LeetCode203
Solution1——不使用虚拟头节点
class Solution {
public ListNode removeElements(ListNode head, int val) {
while(head != null && head.val == val){
ListNode delNode = head;
head = head.next;
delNode.next = null;
}
if(head == null)
return head;
ListNode prev = head;
while(prev.next != null){
if(prev.next.val == val) {
ListNode delNode = prev.next;
prev.next = delNode.next;
delNode.next = null;
}
else
prev = prev.next;
}
return head;
}
}
Solution2——使用虚拟头节点
class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode dummyHead = new ListNode(-1);
dummyHead.next = head;
ListNode prev = dummyHead;
while(prev.next != null){
if(prev.next.val == val)
prev.next = prev.next.next;
else
prev = prev.next;
}
return dummyHead.next;
}
}
4-9 测试自己的LeetCode链表代码
class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode dummyHead = new ListNode(-1);
dummyHead.next = head;
ListNode prev = dummyHead;
while(prev.next != null){
if(prev.next.val == val)
prev.next = prev.next.next;
else
prev = prev.next;
}
return dummyHead.next;
}
public static void main(String[] args) {
int[] nums = {1, 2, 6, 3, 4, 5, 6};
ListNode head = new ListNode(nums);
System.out.println(head);
ListNode res = (new Solution3()).removeElements(head, 6);
System.out.println(res);
}
}
4-10递归基础与递归的宏观语意
递归
- 本质上,将原来的问题转化成为更小的同一问题
- 举例:数组求和
Sum( arr[0…n-1]) = arr[0] + Sum( arr[1…n-1]) ← 更小的同一问题
Sum( arr[1…n-1]) = arr[1] + Sum( arr[2…n-1]) ← 更小的同一问题
…
Sum( arr[n-1…n-1) = arr[n-1] + Sum( [ ] ) ←最基本的问题
public class Sum {
public static int sum(int[] arr){
return sum(arr, 0);
}
private static int sum(int[] arr, int l){
if(l == arr.length)
return 0;
return arr[l] + sum(arr, l + 1);
}
public static void main(String[] args) {
int[] nums = {1, 2, 3, 4, 5, 6, 7, 8};
System.out.println(sum(nums));
}
}

4-11 链表的天然递归结构性质
链表天然的递归性

解决链表中删除元素的问题

class Solution5 {
public ListNode removeElements(ListNode head, int val) {
if(head == null)
return head;
head.next = removeElements(head.next, val);
return head.val == val ? head.next : head;
}
}
4-12 递归运行的机制:递归的微观解读
递归函数的调用,本质就是函数调用,只是调用的函数是自己而已
递归调用是有代价的:函数调用+系统栈空间


