无头单向链表:
class Node {
public int val;
public Node next;
public Node (int val) {
this.val = val;
}
}
public class SingleLinkedList {
Node head;
public SingleLinkedList() {
this.head = null;
}
public void addFirst(int val) {
Node node = new Node(val);
node.next = head;
head = node;
}
public void addLast(int val) {
Node cur = head;
while(cur.next != null) {
cur = cur.next;
}
Node node = new Node(val);
cur.next = node;
}
public boolean addIndex(int index, int val) {
if(index == 0) {
addFirst(val);
return true;
}
if(index == size()) {
addLast(val);
return true;
}
if(index < 0 || index > size()) {
return false;
}
Node cur = head;
for(int i = 0; i < index; i++) {
cur = cur.next;
}
Node node = new Node(val);
node.next = cur.next;
cur.next = node;
return true;
}
public boolean contains (int val) {
Node cur = this.head;
while(cur != null) {
if(cur.val == val) {
return true;
}
cur = cur.next;
}
return false;
}
public boolean remove(int val) {
Node cur = this.head;
Node pre = null;
while(cur != null) {
if(cur.val == val) {
pre.next = cur.next;
return true;
}
pre = cur;
cur = cur.next;
}
return false;
}
public void removeAllKey (int val) {
Node cur = this.head;
Node prev = cur;
if(head.val == val) {
head = head.next;
}
while(cur != null) {
if(cur.val == val) {
prev.next = cur.next;
cur = cur.next;
continue;
}
prev = cur;
cur = cur.next;
}
}
public void clear() {
this.head = null;
}
public int size() {
Node cur = head;
int count = 0;
while(cur != null) {
cur = cur.next;
count++;
}
return count;
}
}
带头双向链表
package Java_0630.MyLinkedList.DoubleLinkedList;
class Node {
int val;
Node prev;
Node next;
public Node(int val) {
this.val = val;
}
}
public class DoubleLinkedList {
Node pHead;
Node head;
Node last;
public DoubleLinkedList() {
pHead = new Node(0);
pHead.next = head;
}
public void addFirtst(int val) {
Node node = new Node(val);
if(head == null) {
this.last = node;
this.head = node;
}else {
node.next = head;
head.prev = node;
head = node;
}
}
public void addLast (int val) {
Node node = new Node(val);
if( this.head == null) {
addFirtst(val);
}else {
this.last.next = node;
node.prev = this.last;
this.last = node;
}
}
public void addIndex(int index, int val) {
if(index == 0) {
addFirtst(val);
} else if(index == size()) {
addLast(val);
} else if(index < 0 || index > size()) {
return;
}
Node cur = this.head;
for(int i = 0; i < index; i++) {
cur = cur.next;
}
Node node = new Node(val);
node.next = cur.next;
node.prev = cur;
cur.next.prev = node;
cur.next = node;
}
public int size() {
Node cur = head;
int count = 0;
while(cur != null) {
count++;
cur = cur.next;
}
return count;
}
}