快慢指针
1. 快慢指针
- 输入链表头结点,奇数长度返回中点,偶数长度返回上中点
java
// head 头
public static Node midOrUpMidNode(Node head) {
if (head == null || head.next == null || head.next.next == null) {
return head;
}
// 链表有3个点或以上
Node slow = head.next;
Node fast = head.next.next;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
阿源的知识库