快慢指针的应用:
1. 快乐数 LeetCode 202
2. 环形链表 LeetCode 141
3. 有序链表的中位数(未找到原题,故自己编写了测试用例↓)
4. 找出链表倒数第k个节点 剑指Offer
参考:https://www.e-learn.cn/content/qita/2102473
【快慢指针方法:求一个有序链表的中位数】算法实现&测试代码:
public class Test {
//【快慢指针————求一个有序链表的中位数】
public static double sortedListMedian(ListNode head) {
if(head == null)
System.out.println("链表不能为空!");
ListNode slow = head, fast = head;
while(fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
if(fast.next == null) //说明链表