代码中的头插尾插非一般头结点之后尾节点之前, 而是仿java.lang.LinkedList中直接操作收尾指针的方式
- 头插: 新节点直接作为头节点
- 尾插: 新节点直接作为尾节点
纯属为大学还债系列, 不多说, 大小朋友们直接看代码吧, 有错指正
/** * 单向列表 * <p> * 仿 java.util.LinkedList 中使用 first last 指针(逻辑指针), 构建单向列表 * * @author wdc */ public class LinkedOneList<T> { transient int size; transient Node<T> first; transient Node<T> last; /** * 头插: 非头结点后插入, 而是new头结点 * * @param value */ public void addHeader(T value) { Node<T> tNode = new Node<>(value, 0, first); if (first == null) { first = last = tNode; ++size; return; } first = tNode; Node<T> next = first.next; while (next != null) { next.index++; next = next.next; } ++size; } /** * 尾插: 非尾节点前插入, 而是last指针后接new节点 * * @param value * @return */ public int addTail(T value) { Node<T> tNode = new Node<T>(value, size, null); if (last == null) { first = last = tNode; ++size; return 0; } last.next = tNode; last = tNode; int index = size; ++size; return index; } /** * 尾插: 辣鸡麻烦写法(纯属自己想写麻烦了) * * @param value * @return */ public int add(T value) { Node<T> newNode; if (first == null) { newNode = new Node<>(value, 0, null); first = last = newNode; } else { if (last != null) { newNode = new Node<>(value, last.index + 1, null); last = last.next = newNode; } else { newNode = new Node<>(value, first.index + 1, null); first.next = last = newNode; } } ++size; return newNode.index; } /** * 指定所以插入 * * @param value * @param index 索引不得超出 链长+1 * @return */ public boolean add(T value, int index) { if (index > size) { throw new IllegalArgumentException("illegal argument index > size"); } if (index < 0) { throw new IllegalArgumentException("index error"); } Node<T> cur = first; if (cur == null) { return false; } Node<T> next = null; if (index == 0) { next = last = first; first = new Node<>(value, index, next); } else if (index > last.index) { last = last.next = new Node<>(value, index, null); } else { do { if (cur.index.equals(index - 1)) { next = cur.next; break; } cur = cur.next; } while (cur != null); if (cur != null) { cur.next = new Node<>(value, index, next); } } if (next != null) { do { next.index++; next = next.next; } while (next != null); } ++size; return true; } public int delete(T value) { if (first == null) { return -1; } Node<T> cur = first; Node<T> tNode = null; Node<T> next; if (first == last) { if (first.value.equals(value)) { first = last = null; size--; return 0; } else { return -1; } } else { if (first.value.equals(value)) { tNode = first.next; first = first.next; next = first; } else { while (cur.next != null) { if (cur.next.value.equals(value)) { tNode = cur.next; break; } cur = cur.next; } if (tNode == null) { return -1; } cur.next = tNode.next; next = tNode.next; if (tNode == last) { last = cur; } } } while (next != null) { next.index--; next = next.next; } size--; return tNode.index; } /** * 判断是否存在环 * <p> * 数学归纳法: 快游标一次两步, 慢游标一次一步 * * @return */ public boolean isExistLoop() { Node<T> fast = first; Node<T> slow = first; while (fast != null && fast.next != null) { fast = fast.next.next;//快的一次走两步 slow = slow.next;//慢的一次走一步 if (fast == slow) {//如果相遇代表该链表有环 return true; } } return false; } public long size() { return size; } @Override public String toString() { StringBuilder builder = new StringBuilder("[ "); Node<T> cur = first; while (cur != null) { builder.append(cur.toString()).append(" "); cur = cur.next; } builder.append("]"); return builder.toString(); } private static class Node<T> { T value; Integer index; // 索引 Node<T> next; public Node(T value, Integer index, Node<T> next) { this.value = value; this.index = index; this.next = next; } @Override public String toString() { return "Node{" + "value=" + value + ", index=" + index + '}'; } } public static void main(String[] args) { LinkedOneList<Integer> list = new LinkedOneList<>(); list.addHeader(1); list.addHeader(2); list.addHeader(3); list.addHeader(4); list.addTail(1); list.addTail(2); list.addTail(3); list.addTail(4); System.out.println(list.toString()); // list.add(1); // list.add(2, 0); // list.add(11); // list.add(11); // list.add(9, 3); // list.add(13, 2); // list.add(45); // System.out.println("链表长度===========> " + list.size()); // // System.out.println(list.toString()); // // System.out.println("被删除元素的索引=====>" + list.delete(1)); // // System.out.println("链表长度===========> " + list.size()); // // System.out.println(list.toString()); } }