public class Node {
Integer value;
Node next;
public Node(Integer value) {
this.value = value;
}
public void print(Node head) {
Node start = head;
StringJoiner str = new StringJoiner(",");
while (start != null) {
str.add(start.value.toString());
start = start.next;
}
System.out.println(str);
}
}
1. 删除链表中的倒数第n个元素
class Test0712 {
@Test
void test01() {
Node head = new Node(0);
head.next = new Node(1);
head.next.next = new Node(2);
head.next.next.next = new Node(3);
head.next.next.next.next = new Node(4);
print(head);
print(reversalLinkedList(head));
}
private Node reversalLinkedList(Node head) {
Node newHead = null;
Node current = head;
while (current != null) {
Node tmp = current.next;
current.next = newHead;
newHead = current;
current = tmp;
}
return newHead;
}
}
2. 链表中环的检测
class Test0712 {
@Test
void test02() {
Node head = new Node(0);
head.next = new Node(1);
head.next.next = new Node(2);
head.next.next.next = new Node(3);
head.next.next.next.next = new Node(4);
System.out.println(hasCycle(head));
}
public static boolean hasCycle(Node head) {
if (head == null || head.next == null) {
return false;
}
Node slow = head;
Node fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
System.out.println(slow.value);
return true;
}
}
return false;
}
}
3. 两个有序的链表合并
class Test0712 {
@Test
void test03() {
Node head = new Node(0);
head.next = new Node(1);
head.next.next = new Node(2);
head.next.next.next = new Node(3);
head.next.next.next.next = new Node(4);
Node head2 = new Node(7);
head2.next = new Node(8);
head2.next.next = new Node(9);
print(head);
print(head2);
print(mergeSortList(head, head2));
}
public Node mergeSortList(Node l1, Node l2) {
if (l1 == null && l2 == null) {
return null;
}
if (l1 == null) {
return l2;
}
if (l2 == null) {
return l1;
}
Node newHead = new Node(null);
Node current = newHead;
while (l1 != null && l2 != null) {
if (l1.value <= l2.value) {
current.next = l1;
l1 = l1.next;
} else {
current.next = l2;
l2 = l2.next;
}
current = current.next;
}
if (l1 != null) {
current.next = l1;
} else {
current.next = l2;
}
return newHead;
}
}
4. 删除链表倒数第 n 个结点(快慢指针)
class Test0712 {
@Test
public void test04() {
Node head = new Node(0);
head.next = new Node(1);
head.next.next = new Node(2);
head.next.next.next = new Node(3);
head.next.next.next.next = new Node(4);
print(head);
print(removeNthFromEnd(head, 2));
}
private Node removeNthFromEnd(Node head, int n) {
Node dummy = new Node(null);
dummy.next = head;
Node first = dummy;
Node second = dummy;
for (int i = 0; i <= n; i++) {
first = first.next;
}
while (first != null) {
first = first.next;
second = second.next;
}
second.next = second.next.next;
return dummy.next;
}
}
5. 求链表的中间结点
class Test0712 {
@Test
public void test05() {
Node head = new Node(0);
head.next = new Node(1);
head.next.next = new Node(2);
head.next.next.next = new Node(3);
head.next.next.next.next = new Node(4);
print(head);
System.out.println(middleNode(head).value);
}
public static Node middleNode(Node head) {
Node slow = head;
Node fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
|