- Reverse linked list; fixed while loop
while(head != null){
next = head.next;
head.next = pre;
pre = head;
head = next;
}
- Delete a fixed node [need to determine whether to delete the head node];
Node preHead = new Node(-1);
preHead.next = head;
pre = ...;
if (head.vaule == ...){
pre.next = pre.next.next;
}
public static Node removeValue(Node head, int num){
while (head != null){
if (head.value != num){
break;
}
head = head.next;
}
Node pre = head;
Node cur = head;
while(cur != null){
if (cur.value == num){
pre.next = cur.next;
} else {
pre = cur;
}
cur = cur.next;
}
}
- Determine whether the singly linked list has a ring
while (slow != fast) {
if (fast == null || fast.next == null) {
return false;
}
slow = slow.next;
fast = fast.next.next;
}
- Find the intermediate node; n1 = n1.next; n2 = n2.next.next; Take one step and two steps
while (slow != fast) {
if (fast == null || fast.next == null) {
return false;
}
slow = slow.next;
fast = fast.next.next;
}
- Sometimes a fake head node can be used to solve the possibility of the head node being deleted and changed
Node fakeNode = new Node(-1);