1.动态链表
动态链表就是链式存储结构具体实现的核心思想
2.头结点与头指针
(1)头结点是指链表中的第一个结点,有真实头结点和虚拟头结点之分
真实头结点:其第一个结点用于存储数据
虚拟头结点:其第一个结点不许存储数据
(2)头指针:仅仅是一个引用变量,存储头结点地址的指针而已
(3)尾指针:同头指针,不过是链表中最后一个结点的指针而已
3.单向循环链表
如果把单链表的最后一个节点的指针指向链表头部,而不是指向NULL,那么就构成了一个单向循环链表
4.双向循环链表
双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。一般我们都构造双向循环链表。
代码实现
1.单向链表
package p3.链式结构;
import p1.接口.List;
import java.util.Comparator;
import java.util.Iterator;
//单向链表
public class LinkedSinglyList implements List {
//定义结点对象
private class Node {
E data; //数据域
Node next; //指针域
public Node() {
this(null,null);
}
public Node(E data) {
this(data,null);
}
public Node(E data,Node next) {
this.data = data;
this.next = next;
}
@Override
public String toString() {
return data.toString();
}
}
private Node head; //头指针
private Node tail; //尾指针
private int size; //元素的个数
public LinkedSinglyList() {
head = null;
tail = null;
size = 0;
}
public LinkedSinglyList(E[] arr) {
if (arr == null || arr.length == 0) {
throw new IllegalArgumentException("arr is null");
}
for (int i = 0; i < arr.length; i++) {
add(arr[i]);
}
}
@Override
public void add(E element) {
add(size,element);
}
@Override
public void add(int index, E element) {
if (index < 0 || index > size) {
throw new IllegalArgumentException("add index out of range");
}
Node n = new Node(element);
if (size == 0) {
head = n;
tail = n;
} else if (index == 0) {
n.next = head;
head = n;
} else if (index == size) {
tail.next = n;
tail = n;
} else {
Node p = head;
for (int i = 0; i < index - 1; i++) {
p = p.next;
}
n.next = p.next;
p.next = n;
}
size++;
}
@Override
public void remove(E element) {
int index = indexOf(element);
if (index != -1) {
remove(index);
}
}
@Override
public E remove(int index) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("remove index out of range");
}
E ret = null;
if (size == 1) {
ret = head.data;
head = null;
tail = null;
} else if (index == 0) {
Node n = head;
ret = n.data;
head = n.next;
n.next = null;
} else if (index == size - 1) {
Node p = head;
while (p.next != tail) {
p = p.next;
}
ret = tail.data;
p.next = null;
tail = p;
} else {
Node p = head;
for (int i = 0; i < index - 1; i++) {
p = p.next;
}
Node n = p.next;
ret = n.data;
p.next = n.next;
n.next = null;
}
size--;
return ret;
}
@Override
public E get(int index) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("get index out of range");
}
if (index == 0) {
return head.data;
} else if (index == size - 1) {
return tail.data;
} else {
Node p = head;
for (int i = 0; i < index; i++) {
p = p.next;
}
return p.data;
}
}
@Override
public E set(int index, E element) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("set index out of range");
}
E ret = null;
if (index == 0) {
ret = head.data;
head.data = element;
} else if(index == size - 1) {
ret = tail.data;
tail.data = element;
} else {
Node p = head;
for (int i = 0; i < index; i++) {
p = p.next;
}
ret = p.data;
p.data = element;
}
return ret;
}
@Override
public int size() {
return size;
}
@Override
public int indexOf(E element) {
Node p = null;
int index = 0;
while (!p.data.equals(element)) {
p = p.next;
index++;
if (p == null) {
return -1;
}
}
return index;
}
@Override
public boolean contains(E element) {
return indexOf(element) != -1;
}
@Override
public boolean isEmpty() {
return size == 0 && head == null && tail == null;
}
@Override
public void clear() {
head = null;
tail = null;
size = 0;
}
@Override
public void sort(Comparator c) {
if (c == null) {
throw new IllegalArgumentException("comparator can not be null");
}
//此处的插入排序O(n^3)
/*
for (int i = 1; i < size; i++) {
E e = get(i);
int j = 0;
for (j = i; j > 0 && c.compare(get(j - 1), e) > 0; j--) {
set(j, get(j - 1));
}
set(j, e);
}
*/
if (size == 0 || size == 1) {
return;
}
Node nodeA = head;
Node nodeB = nodeA.next;
while (true) {
while (true) {
if (c.compare(nodeA.data, nodeB.data) > 0) {
swap(nodeA,nodeB);
}
if (nodeB == tail) {
break;
}
nodeB = nodeB.next;
}
if (nodeA.next == tail) {
break;
}
nodeA = nodeA.next;
nodeB = nodeA.next;
}
}
private void swap(Node nodeA, Node nodeB) {
E temp = nodeA.data;
nodeA.data = nodeB.data;
nodeB.data = temp;
}
@Override
public List subList(int fromIndex, int toIndex) {
//0 <= fromIndex <= toIndex <= size - 1 [fromIndex,toIndex]
if (fromIndex < 0 || toIndex >= size || fromIndex > toIndex) {
throw new IllegalArgumentException("must 0 <= fromIndex <= toIndex <= size - 1");
}
LinkedSinglyList list = new LinkedSinglyList<>();
/*
for (int i = fromIndex; i <= toIndex; i++) { //O(M)
list.add(get(i)); //O(N) * O(M) = O(NM) ~ O(N^2)
}
*/
Node nodeA = head;
for (int i = 0; i < fromIndex; i++) {
nodeA = nodeA.next;
}
Node nodeB = head;
for (int i = 0; i< toIndex; i++) {
nodeB = nodeB.next;
}
Node p =nodeA;
while (true) {
list.add(p.data);
if (p == nodeB) {
break;
}
p = p.next;
}
return list;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append('[');
if (isEmpty()) {
sb.append(']');
} else {
Node p = head;
while (true) {
sb.append(p.data);
if (p == tail) {
sb.append(']');
break;
} else {
sb.append(',');
sb.append(' ');
}
p = p.next;
}
}
return sb.toString();
}
@Override
public Iterator iterator() {
return new LinkedSinglyListIterator();
}
class LinkedSinglyListIterator implements Iterator {
private Node cur = head;
@Override
public boolean hasNext() {
return cur != null;
}
@Override
public E next() {
E ret = cur.data;
cur = cur.next;
return ret;
}
}
}
2..单向循环链表
package p3.链式结构;
import p1.接口.List;
import java.util.Comparator;
import java.util.Iterator;
//单向循环链表
public class LinkedSinglyCircularList implements List {
//定义结点对象
private class Node {
E data; //数据域
Node next; //指针域
public Node() {
this(null,null);
}
public Node(E data) {
this(data,null);
}
public Node(E data, Node next) {
this.data = data;
this.next = next;
}
@Override
public String toString() {
return data.toString();
}
}
private Node head; //头指针
private Node tail; //尾指针
private int size; //元素的个数
public LinkedSinglyCircularList() {
head = null;
tail = null;
size = 0;
}
public LinkedSinglyCircularList(E[] arr) {
if (arr == null || arr.length == 0) {
throw new IllegalArgumentException("arr is null");
}
for (int i = 0; i < arr.length; i++) {
add(arr[i]);
}
}
@Override
public void add(E element) {
add(size,element);
}
@Override
public void add(int index, E element) {
if (index < 0 || index > size) {
throw new IllegalArgumentException("add index out of range");
}
Node n = new Node(element);
if (size == 0) {
head = n;
tail = n;
tail.next = head; //new code
} else if (index == 0) {
n.next = head;
head = n;
tail.next = head; //new code
} else if (index == size) {
n.next = head; //new code
tail.next = n;
tail = n;
} else {
Node p = head;
for (int i = 0; i < index - 1; i++) {
p = p.next;
}
n.next = p.next;
p.next = n;
}
size++;
}
@Override
public void remove(E element) {
int index = indexOf(element);
if (index != -1) {
remove(index);
}
}
@Override
public E remove(int index) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("remove index out of range");
}
E ret = null;
if (size == 1) {
ret = head.data;
head = null;
tail = null;
} else if (index == 0) {
Node n = head;
ret = n.data;
head = n.next;
n.next = null;
tail.next = head; //new code
} else if (index == size - 1) {
Node p = head;
while (p.next != tail) {
p = p.next;
}
ret = tail.data;
p.next = tail.next; //change code
tail = p;
} else {
Node p = head;
for (int i = 0; i < index - 1; i++) {
p = p.next;
}
Node n = p.next;
ret = n.data;
p.next = n.next;
n.next = null;
}
size--;
return ret;
}
@Override
public E get(int index) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("get index out of range");
}
if (index == 0) {
return head.data;
} else if (index == size - 1) {
return tail.data;
} else {
Node p = head;
for (int i = 0; i < index; i++) {
p = p.next;
}
return p.data;
}
}
@Override
public E set(int index, E element) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("set index out of range");
}
E ret = null;
if (index == 0) {
ret = head.data;
head.data = element;
} else if(index == size - 1) {
ret = tail.data;
tail.data = element;
} else {
Node p = head;
for (int i = 0; i < index; i++) {
p = p.next;
}
ret = p.data;
p.data = element;
}
return ret;
}
@Override
public int size() {
return size;
}
@Override
public int indexOf(E element) {
Node p = null;
int index = 0;
while (!p.data.equals(element)) {
p = p.next;
index++;
if (p == null) {
return -1;
}
}
return index;
}
@Override
public boolean contains(E element) {
return indexOf(element) != -1;
}
@Override
public boolean isEmpty() {
return size == 0 && head == null && tail == null;
}
@Override
public void clear() {
head = null;
tail = null;
size = 0;
}
@Override
public void sort(Comparator c) {
if (c == null) {
throw new IllegalArgumentException("comparator can not be null");
}
//此处的插入排序O(n^3)
/*
for (int i = 1; i < size; i++) {
E e = get(i);
int j = 0;
for (j = i; j > 0 && c.compare(get(j - 1), e) > 0; j--) {
set(j, get(j - 1));
}
set(j, e);
}
*/
if (size == 0 || size == 1) {
return;
}
Node nodeA = head;
Node nodeB = nodeA.next;
while (true) {
while (true) {
if (c.compare(nodeA.data, nodeB.data) > 0) {
swap(nodeA,nodeB);
}
if (nodeB == tail) {
break;
}
nodeB = nodeB.next;
}
if (nodeA.next == tail) {
break;
}
nodeA = nodeA.next;
nodeB = nodeA.next;
}
}
private void swap(Node nodeA, Node nodeB) {
E temp = nodeA.data;
nodeA.data = nodeB.data;
nodeB.data = temp;
}
@Override
public List subList(int fromIndex, int toIndex) {
//0 <= fromIndex <= toIndex <= size - 1 [fromIndex,toIndex]
if (fromIndex < 0 || toIndex >= size || fromIndex > toIndex) {
throw new IllegalArgumentException("must 0 <= fromIndex <= toIndex <= size - 1");
}
LinkedSinglyList list = new LinkedSinglyList<>();
/*
for (int i = fromIndex; i <= toIndex; i++) { //O(M)
list.add(get(i)); //O(N) * O(M) = O(NM) ~ O(N^2)
}
*/
Node nodeA = head;
for (int i = 0; i < fromIndex; i++) {
nodeA = nodeA.next;
}
Node nodeB = head;
for (int i = 0; i< toIndex; i++) {
nodeB = nodeB.next;
}
Node p =nodeA;
while (true) {
list.add(p.data);
if (p == nodeB) {
break;
}
p = p.next;
}
return list;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append('[');
if (isEmpty()) {
sb.append(']');
} else {
Node p = head;
while (true) {
sb.append(p.data);
if (p == tail) {
sb.append(']');
break;
} else {
sb.append(',');
sb.append(' ');
}
p = p.next;
}
}
return sb.toString();
}
@Override
public Iterator iterator() {
return new LinkedSinglyCircularListIterator();
}
class LinkedSinglyCircularListIterator implements Iterator {
private Node cur = head;
private boolean flag = true; //是否在第一圈
@Override
public boolean hasNext() {
if (isEmpty()) {
return false;
}
return flag;
}
@Override
public E next() {
E ret = cur.data;
cur = cur.next;
if (cur == head) {
flag = false;
}
return ret;
}
}
}
3.双向循环链表
package p3.链式结构;
import p1.接口.Dequeue;
import p1.接口.List;
import p1.接口.Stack;
import java.util.Comparator;
import java.util.Iterator;
//双向循环链表
public class LinkedList implements List, Dequeue, Stack {
private class Node {
E data;
Node pre; //直接前驱
Node next; //直接后继
public Node() {
this(null,null,null);
}
public Node(E data) {
this(data,null,null);
}
public Node(E data, Node pre, Node next) {
this.data = data;
this.pre = pre;
this.next = next;
}
@Override
public String toString() {
return data.toString();
}
}
private Node head;
private Node tail;
private int size;
public LinkedList() {
head = null;
tail = null;
size = 0;
}
public LinkedList(E[] arr) {
if (arr == null) {
throw new IllegalArgumentException("arr can not be null");
}
for (E e : arr) {
add(e);
}
}
@Override
public void add(E element) {
add(size,element);
}
@Override
public void add(int index, E element) {
if (index < 0 || index > size) {
throw new IllegalArgumentException("add index out of range");
}
Node n= new Node(element);
if (size == 0) {
head = n;
tail = n;
tail.next = head;
head.pre = tail;
} else if (index == 0) {
n.pre = head.pre;
n.next = head;
head.pre = n;
head = n;
tail.next = head;
} else if (index == size) {
n.next = tail.next;
tail.next = n;
n.pre = tail;
tail = n;
head.pre = tail;
} else {
Node p, q;
if (index <= size / 2){
p = head;
for (int i = 0; i < index; i++) {
p = p.next;
}
q = p.next;
p.next = n;
n.pre =p;
q.pre = n;
n.next = q;
} else {
p = tail;
for (int i = size - 1; i > index; i--) {
p = p.pre;
}
q = p.pre;
q.next = n;
n.pre = q;
n.next = p;
p.pre = n;
}
}
size++;
}
@Override
public void remove(E element) {
int index = indexOf(element);
if (index != -1) {
remove(index);
}
}
@Override
public E remove(int index) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("remove index out of range");
}
E ret = null;
Node node;
if (size == 1) {
ret = head.data;
head = null;
tail = null;
} else if (index == 0) {
ret = head.data;
node = head.next;
head.next = null;
node.pre = head.pre;
head.pre = null;
head = node;
tail.next = head;
} else if (index == size - 1) {
ret = tail.data;
node = tail.pre;
tail.pre = null;
node.next = tail.next;
tail.next = null;
tail = node;
head.pre = tail;
} else {
Node p,q,r;
if (index <= size / 2) {
p = head;
for (int i = 0; i < index - 1; i++) {
p = p.next;
}
q = p.next;
r = q.next;
ret = q.data;
p.next = r;
r.pre = p;
q.next = null;
q.pre = null;
} else {
p = tail;
for (int i = size - 1; i > index + 1; i++) {
p = p.pre;
}
q = p.pre;
r = q.pre;
ret = p.data;
r.next = p;
p.pre = r;
q.next = null;
q.pre = null;
}
}
size--;
return ret;
}
@Override
public E get(int index) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("get index out of range");
}
if (index == 0) {
return head.data;
} else if (index == size - 1) {
return tail.data;
} else {
Node p = head;
for (int i = 0;i < index; i++) {
p = p.next;
}
return p.data;
}
}
@Override
public E set(int index, E element) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("set index out of range");
}
E ret = null;
if (index == 0) {
ret = head.data;
head.data = element;
} else if (index == size - 1) {
ret = tail.data;
tail.data = element;
} else {
Node p = head;
for (int i = 0;i < index; i++) {
p = p.next;
}
ret = p.data;
p.data = element;
}
return ret;
}
@Override
public int size() {
return size;
}
@Override
public int indexOf(E element) {
Node p = head;
int index = 0;
while (!p.data.equals(element)) {
p = p.next;
index++;
if (p == head) {
return -1;
}
}
return index;
}
@Override
public boolean contains(E element) {
return indexOf(element) != -1;
}
@Override
public boolean isEmpty() {
return size == 0 && head == null && tail == null;
}
@Override
public void clear() {
head = null;
tail = null;
size = 0;
}
@Override
public void sort(Comparator c) {
if (c == null) {
throw new IllegalArgumentException("comparator can not be null");
}
//插入排序来做
if (size == 0 || size == 1) {
return;
}
for (Node nodeA = head.next; nodeA != head; nodeA = nodeA.next) {
E e = nodeA.data;
Node nodeB;
Node nodeC;
for (nodeB = nodeA,nodeC = nodeB.pre; nodeC != tail && c.compare(nodeC.data,e) > 0; nodeB = nodeB.pre, nodeC = nodeC.pre) {
nodeB.data = nodeC.data;
}
nodeB.data = e;
}
}
@Override
public List subList(int fromIndex, int toIndex) {
if (fromIndex < 0 || toIndex >= size || fromIndex > toIndex) {
throw new IllegalArgumentException("0 <= fromIndex <= toIndex < size");
}
Node nodeA =head;
for (int i = 0; i < fromIndex; i++) {
nodeA = nodeA.next;
}
Node nodeB = head;
for (int i = 0; i < toIndex; i++) {
nodeB = nodeB.next;
}
Node p = nodeA;
LinkedList list = new LinkedList<>();
while (true) {
list.add(p.data);
if (p == nodeB) {
break;
}
p = p.next;
}
return list;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append('[');
if (isEmpty()) {
sb.append(']');
} else {
Node p = head;
while (true) {
sb.append(p.data);
if (p == tail) {
sb.append(']');
break;
}
sb.append(',');
sb.append(' ');
p = p.next;
}
}
return sb.toString();
}
@Override
public Iterator iterator() {
return new LinkedListIterator();
}
class LinkedListIterator implements Iterator {
private Node cur = head;
private boolean flag = true; //是否在第一圈
@Override
public boolean hasNext() {
if (isEmpty()) {
return false;
}
return flag;
}
@Override
public E next() {
E ret = cur.data;
cur = cur.next;
if (cur == head) {
flag = false;
}
return ret;
}
}
//双端队列的方法
@Override
public void addFirst(E element) {
add(0,element);
}
@Override
public void addLast(E element) {
add(size,element);
}
@Override
public E removeFirst() {
return remove(0);
}
@Override
public E reomveLast() {
return remove(size - 1);
}
@Override
public E getFirst() {
return get(0);
}
@Override
public E getLast() {
return get(size - 1);
}
//栈的方法
@Override
public void push(E element) {
addLast(element);
}
@Override
public E pop() {
return reomveLast();
}
@Override
public E peek() {
return getLast();
}
//队列的方法
@Override
public void offer(E element) {
addLast(element);
}
@Override
public E poll() {
return removeFirst();
}
@Override
public E element() {
return getFirst();
}
}
4.约瑟夫环问题
(1)从单向循环链表的内部来处理 处理结点与结点之间的关系
public void josephusLoop() {
if (size <= 2) {
return;
}
Node p = head;
while (size != 2) {
p = p.next;
Node del = p.next;
if (del == head) {
head = del.next;
} else if (del == tail) {
tail = p;
}
p.next = del.next;
del.next = null;
p = p.next;
size--;
}
}
(2)从单向循环链表的外部来处理 处理就是角标之间的关系
int index = 0;
while (list.size() != 2) {
index = (index + 2) % list.size();
list.remove(index);
}
5.链表反转问题
public void reverse() {
if (size == 0 || size == 1) {
return;
}
Node dummpyHead = new Node(); //虚拟头结点
Node p = head;
for (int i = 0; i < size; i++) {
Node n = new Node(p.data);
if (dummpyHead.next == null) {
tail = n;
}
n.next = dummpyHead.next;
dummpyHead.next = n;
p= p.next;
}
head = dummpyHead.next;
}
6.逢七过游戏
package p3.链式结构;
import java.util.ArrayList;
import java.util.Scanner;
//逢七过游戏
/*
输入玩家的个数
输入从哪个玩家开始
输入该玩家从哪个数字开始
输入一共玩几个数字
打印出每个玩家将要报出的所有数字
*/
public class SevenGame {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print(">>>请输入玩家的个数: ");
int playerCount = input.nextInt();
System.out.print(">>>请输入从哪个玩家开始: ");
int beginPlayer = input.nextInt();
System.out.print(">>>请输入从哪个数字开始: ");
int beginNumber = input.nextInt();
System.out.print(">>>请输入数字的最大值: ");
int maxNumber = input.nextInt();
//创建玩家的集合
LinkedSinglyCircularList> list = new LinkedSinglyCircularList<>();
//分别创建玩家的对象
for (int i = 0; i < playerCount; i++) {
list.add(new ArrayList<>());
}
//开始玩家的角标
int index = beginPlayer - 1;
//将数字 依次分给每一个玩家
for (int num = beginNumber; num <= maxNumber; num++) {
list.get(index++ % playerCount).add(getAnswer(num));
}
for (int i = 0; i < list.size(); i++) {
System.out.println("第" + (i + 1) + "位玩家: " + list.get(i));
}
}
private static String getAnswer(int num) {
if (num % 7 == 0 || (num + "").contains("7")) {
return "过";
}
return num + "";
}
}
7.删除链表的倒数第N个结点
package p3.链式结构;
//删除链表的倒数第N个结点
public class Solution19 {
public ListNode removeNthFromEnd(ListNode head, int n) {
if (head == null || head.next == null) {
return null;
}
ListNode right = head;
ListNode left = head;
for (int i = 0;i < n; i ++) {
right = right.next;
}
if (right == null) {
return head.next;
}
while (right.next != null) {
right = right.next;
left = left.next;
}
ListNode del = left.next;
left.next = del.next;
del.next = null;
return head;
}
}
8.删除排序链表中的重复元素
package p3.链式结构;
//删除排序链表中的重复元素
public class Solution83 {
public ListNode deleteDuplicates(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode left = head;
ListNode right = left.next;
while (right != null) {
if (left.val == right.val) {
left.next = right.next;
right.next = null;
right = left.next;
} else {
left = left.next;
right = left.next;
}
}
return head;
}
}
9.删除排序链表中的重复元素II
package p3.链式结构;
import p1.接口.List;
//删除排序链表中的重复元素II
public class Solution82 {
public ListNode deleteDuplicates(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode pre = dummy;
ListNode cur = head;
while (cur != null && cur.next != null) {
if (cur.val != cur.next.val) {
pre = cur;
cur = cur.next;
} else {
ListNode next = cur.next.next;
while (next != null && next.val == cur.val) {
next = next.next;
}
pre.next = next;
cur = next;
}
}
return dummy.next;
}
}
10.环形链表
package p3.链式结构;
//环形链表
public class Solution141 {
public boolean hasCycle(ListNode head) {
if (head == null || head.next == null) {
return false;
}
ListNode p = head;
ListNode q = head;
while (q.next != null && q.next.next != null) {
p = p.next;
q = q.next.next;
if (p == q) {
return true;
}
}
return false;
}
}
11.环形链表II
package p3.链式结构;
import p1.接口.List;
//环形链表II
public class Solution142 {
public ListNode detectCycle(ListNode head) {
if (head == null || head.next == null) {
return null;
}
ListNode slow = head;
ListNode fast = head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
ListNode cur = head;
while (cur != slow) {
cur = cur.next;
slow = slow.next;
}
return cur;
}
}
return null;
}
}