一、链表的概念和结构
1.1 链表的概念
1.2 链表的分类
二、单向不带头非循环链表
2.1 创建节点类型
class ListNode { public int val;//数值 public ListNode next;//下一个节点的地址 public ListNode(int val) { this.val = val; } }
我们在 MyLinkedList 里创建一个head变量来标识链表的头部,接着就是实现单链表的增删查改了
2.2 头插法
这个头插法并不要考虑第一次插入,每次插入只需要把插入的节点node 的next值改成头节点,再把头节点指向node
//头插法 public void addFirst(int data) { ListNode node = new ListNode(data); node.next = this.head; this.head = node; }
2.3 尾插法
//尾插法 public void addLast(int data) { ListNode node = new ListNode(data); ListNode cur = this.head; //第一次插入 if(this.head == null) { this.head = node; }else{ while (cur.next != null) { cur = cur.next; } cur.next = node; } }
2.4 获取链表长度
//得到单链表的长度 public int size() { int count = 0; ListNode cur = this.head; while (cur != null) { cur = cur.next; count++; } return count; }
2.5 任意位置插入
最关键的就是从中间任意位置插入 要从中间位置插入,就需要找到要插入位置的前一个节点的位置。再插入到它们中间。
/** * 让 cur 向后走 index - 1 步 * @param index * @return */ public ListNode findIndexSubOne(int index) { int count = 0; ListNode cur = this.head; while (count != index-1) { cur = cur.next; count++; } return cur; } //任意位置插入,第一个数据节点为0号下标 public void addIndex(int index,int data) { //判断合法性 if(index < 0 || index > size()) { System.out.println("index位置不合法"); return; } //头插法 if(index == 0) { this.addFirst(data); return; } //尾插法 if(index == size()) { this.addLast(data); return; } //找前驱,cur指向的是 index 的前一个节点 ListNode cur = findIndexSubOne(index); ListNode node = new ListNode(data); node.next = cur.next; cur.next = node; }
2.6 查找关键字
//查找是否包含关键字key是否在单链表当中 public boolean contains(int key) { ListNode cur = this.head; while (cur != null) { if(cur.val == key) { return true; } cur = cur.next; } return false; }
2.7 删除第一次出现值为key的节点
这个思路其实也很简单,考虑到两种情况即可
/** * 找要删除 key 的前一个节点 * @return */ public ListNode searchPrev(int key) { ListNode prev = this.head; while (prev.next != null) { if (prev.next.val == key) { return prev; } prev = prev.next; } return null; } //删除第一次出现关键字为key的节点 public void remove(int key) { if(this.head.val == key) { this.head = this.head.next; return; } //找 key 的前驱节点 ListNode prev = searchPrev(key); if(prev == null) { System.out.println("没有key这个关键字"); return; } //删除 ListNode delete = prev.next; prev.next = delete.next; }
2.8 删除所有值为key的节点
假设要删除的是3,思路:
记住一定要边画图边写代码!
//删除所有值为key的节点 public void removeAllKey(int key) { ListNode prev = this.head; ListNode cur = this.head.next; while (cur != null) { if(cur.val == key) { prev.next = cur.next; cur = cur.next; }else { prev = cur; cur = cur.next; } } //判断第一个节点是否是要删除的节点 if(this.head.val == key) { this.head = this.head.next; } }
2.9 遍历打印链表
//打印链表 public void display() { ListNode cur = this.head; while (cur != null) { System.out.print(cur.val+" "); cur = cur.next; } System.out.println(); }
置空链表
//置空链表 public void clear() { ListNode cur = this.head; //一个个制空 while (cur != null) { ListNode curNext = cur.next; cur.next = null; cur = curNext; } this.head = null; }
三、双向不带头非循环链表
public class TestLinkedList { public ListNode head; public ListNode last; }
3.1 创建节点类型
同样先定义节点类型,比单向链表多了一个前驱节点而已。
class ListNode { public int val; public ListNode prev; public ListNode next; public ListNode (int val) { this.val = val; } }
3.2 头插法
//头插法 public void addFirst(int data) { ListNode node = new ListNode(data); //第一次插入 if(this.head == null) { this.head = node; this.last = node; }else { head.prev = node; node.next = this.head; this.head = node; } }
3.3 尾插法
//尾插法 public void addLast(int data) { ListNode node = new ListNode(data); //第一次插入 if(this.head == null) { this.head = node; this.last = node; }else { this.last.next = node; node.prev = this.last; this.last = node; } }
3.4 获取链表长度
//得到链表的长度 public int size() { ListNode cur = this.head; int count = 0; while (cur != null) { count++; cur = cur.next; } return count; }
3.5 任意位置插入
任意位置插入也和单链表类似有三种情况。判断合法性和头插尾插就不多了主要还是在中间的随机插入,一定要注意修改的顺序!
要修改的地方一共有四个,一定要画图理解!
//找要插入的节点的位置 public ListNode searchIndex(int index) { ListNode cur = this.head; while (index != 0) { cur = cur.next; index--; } return cur; } //任意位置插入,第一个数据节点为0号下标 public void addIndex(int index,int data) { //判断index位置的合法性 if(index < 0 || index > this.size()) { System.out.println("index的位置不合法"); return; } //头插法 if(index == 0) { this.addFirst(data); return; } //尾插法 if(index == this.size()) { this.addLast(data); return; } //中间插入 ListNode node = new ListNode(data); ListNode cur = searchIndex(index); node.next = cur; node.prev = cur.prev; cur.prev.next = node; cur.prev = node; }
3.6 查找关键字
//查找是否包含关键字key是否在单链表当中 public boolean contains(int key) { ListNode cur = this.head; while (cur != null) { if(cur.val == key) { return true; } cur = cur.next; } return false; }
3.7 删除第一次出现的关键字key的节点
//删除第一次出现关键字为key的节点 public void remove(int key) { ListNode cur = this.head; while (cur != null) { if(cur.val == key) { //要删除的是头节点 if(this.head == cur) { this.head = this.head.next; this.head.prev = null; }else { //尾巴节点和中间的节点两种情况 cur.prev.next = cur.next; if(this.last == cur) { //删除尾巴节点 cur = cur.prev; }else { cur.next.prev = cur.prev; } } //已经删完了 return; }else { cur = cur.next; } } }
3.8 删除所有值为key的节点
思路和删除一个key类似,但需要注意两个点。
//删除所有值为key的节点 public void removeAllKey(int key) { ListNode cur = this.head; while (cur != null) { if(cur.val == key) { //要删除的是头节点 if(this.head == cur) { this.head = this.head.next; //假设全部是要删除的节点 if(this.head != null) { this.head.prev = null; }else { //防止最后一个节点不能被回收 this.last = null; } }else { //尾巴节点和中间的节点两种情况 cur.prev.next = cur.next; if(this.last == cur) { //删除尾巴节点 cur = cur.prev; }else { cur.next.prev = cur.prev; } } //走一步 cur = cur.next; }else { cur = cur.next; } } }
3.9 遍历打印链表
//打印链表 public void display() { ListNode cur = this.head; while (cur != null) { System.out.print(cur.val+" "); cur = cur.next; } System.out.println(); }
置空链表
//置空链表 public void clear() { ListNode cur = this.head; //一个一个置空 while (cur != null) { ListNode curNext = cur.next; cur.prev = null; cur.next = null; cur = curNext; } this.head = null; this.last = null; }
四、总结
1.这里实现了两种较难的链表:单向不带头非循环和双向不带头非循环
2.链表物理上不一定连续,但逻辑上一定连续。
3.增:链表插入一个元素只需要修改指向,所以时间复杂度为O(1)
4:删:链表删除元素,同样只需修改指向,时间复杂度为O(1)
5.查:链表如果需要查找一个元素需要遍历链表,所以时间复杂度为O(n)
6.改:链表要去找到要修改的元素,所以时间复杂度为O(n).