class Node {
public int data;
public Node next;
public Node() {
}
public Node(int data) {
this.data = data;
}
}
class LinkedList {
public Node head;
public void addFirst(int data) {
Node node = new Node(data);
if (this.head == null) {
this.head = node;
} else {
node.next = this.head;
this.head = node;
}
}
public void addLast(int data) {
Node node = new Node(data);
if (this.head == null) {
this.head = node;
} else {
Node cur = this.head;
while (cur.next != null) {
cur = cur.next;
}
cur.next = node;
}
}
public Node searchIndex(int index) {
int count = 0;
Node cur = this.head;
while (count < index - 1) {
cur = cur.next;
count++;
}
return cur;
}
public void checkIndex(int index) {
if (index < 0 || index > getLength()) {
throw new
IndexOutOfBoundsException("下標不合法");
}
}
private int getLength() {
int count = 0;
Node cur = this.head;
while (cur != null) {
cur = cur.next;
count++;
}
return count;
}
public boolean addIndex(int index, int data) {
checkIndex(index);
if (index == 0) {
addFirst(data);
return true;
}
Node cur = searchIndex(index);
Node node = new Node(data);
node.next = cur.next;
cur.next = node;
return true;
}
public void display() {
Node cur = this.head;
while (cur != null) {
System.out.print(cur.data + " ");
cur = cur.next;
}
System.out.println();
}
public void remove(int key) {
if (this.head.data == key) {
this.head = this.head.next;
}
Node prev = searchPrev(key);
if (prev == null) {
return;
}
Node del = prev.next;
prev.next = del.next;
}
private Node searchPrev(int key) {
Node prev = this.head;
while (prev.next != null) {
if (prev.next.data == key) {
return prev;
}
prev = prev.next;
}
return null;
}
public void removeAll(int key) {
Node prev = this.head;
Node cur = this.head.next;
while (cur != null) {
if (prev.next.data == key) {
prev.next = cur.next;
cur = prev.next;
} else {
prev = cur;
cur = prev.next;
}
}
if (this.head.data == key) {
this.head = this.head.next;
}
}
public void display1(Node newHead) {
Node cur = newHead;
while (cur != null) {
System.out.print(cur.data + " ");
cur = cur.next;
}
System.out.println();
}
public Node findMidNode() {
Node fast = this.head;
Node slow = this.head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
public Node findKthToTail(int k) {
if (k <= 0) {
return null;
}
Node fast = this.head;
Node slow = this.head;
while (k - 1 > 0) {
if (fast.next != null) {
fast = fast.next;
k--;
} else {
System.out.println("沒有此節點");
return null;
}
}
while (fast.next != null) {
fast = fast.next;
slow = slow.next;
}
return slow;
}
public int size(Node head) {
int size = 0;
Node cur = head;
while (cur != null) {
size++;
cur = cur.next;
}
return size;
}
public Node deleteDuplication() {
Node newHead = new Node(-1);
Node tem = newHead;
Node cur = this.head;
while (cur != null) {
if (cur.next != null && cur.data == cur.next.data) {
while (cur.next != null && cur.data == cur.next.data) {
cur = cur.next;
}
cur = cur.next;
tem.next = cur;
} else {
tem.next = cur;
tem = tem.next;
cur = cur.next;
}
}
return newHead;
}
public Node reversList() {
Node prev = null;
Node cur = this.head;
Node newHe1ad = null;
while (cur != null) {
Node curNext = cur.next;
if (curNext == null) {
newHe1ad = cur;
}
cur.next = prev;
prev = cur;
cur = curNext;
}
return newHe1ad;
}
public Node partition(int x) {
Node beforeStart = null;
Node beforeEnd = null;
Node afterStart = null;
Node afterEnd = null;
Node cur = this.head;
while (cur != null) {
Node curNext = cur.next;
cur.next = null;
if (cur.data < x) {
if (beforeStart == null) {
beforeStart = cur;
beforeEnd = cur;
} else {
beforeEnd.next = cur;
beforeEnd = cur;
}
} else {
if (afterStart == null) {
afterStart = cur;
afterEnd = cur;
} else {
afterEnd.next = cur;
afterEnd = cur;
}
}
cur = curNext;
}
if (beforeStart == null) {
return afterStart;
}
beforeEnd.next = afterStart;
return beforeStart;
}
public boolean chkPalindrome() {
if (this.head == null) {
return false;
}
if (this.head.next == null) {
return true;
}
Node fast = this.head;
Node slow = this.head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
Node p = slow.next;
while (p != null) {
Node pNext = p.next;
p.next = slow;
slow = p;
p = pNext;
if (p != null) {
pNext = p.next;
}
}
while (this.head != slow) {
if (this.head.data != slow.data) {
return false;
}
if (this.head.next == slow) {
return true;
}
this.head = this.head.next;
slow = slow.next;
}
return true;
}
public void createCycle() {
Node cur = this.head;
while (cur.next != null) {
cur = cur.next;
}
cur.next = this.head.next.next;
}
public boolean hasCycle() {
Node fast = this.head;
Node slow = this.head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if (fast == slow) {
return true;
}
}
return false;
}
public Node detectCycle() {
Node fast = this.head;
Node slow = this.head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if (fast == slow) {
break;
}
}
if (fast == null || fast.next == null) {
return null;
}
fast = this.head;
while (fast != slow) {
fast = fast.next;
slow = slow.next;
}
return fast;
}
}