876. 链表的中间结点 思路:快慢指针
解题思路
如果先遍历再查找中间节点,那么随着数据量增大将非常消耗性能!
利用快慢指针非常巧妙,类似使用取模方式之巧妙!2倍则利用2倍快指针,3的倍数则利用3倍快指针,以此类推。
代码
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode middleNode(ListNode head) {
// head为空或者只有1个、2个节点的情况都包含在内
ListNode fast = head, slow = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
}
测试用例
// 测试用例
public void test() {
int arr1[] = {1};
test(arr1);
int arr2[] = {1, 2};
test(arr2);
int arr3[] = {1, 2, 3};
test(arr3);
int arr4[] = {1, 2, 3, 4};
test(arr4);
int arr5[] = {1, 2, 3, 4, 5};
test(arr5);
int arr6[] = {1, 2, 3, 4, 5, 6};
test(arr6);
int arr7[] = {1, 2, 3, 4, 5, 6, 7};
test(arr7);
test(null);
}
public void test(int arr[]) {
ListNode head = arr == null ? null : new ListNode(arr);
System.out.println("组合完成:" + (head == null ? "【null】" : head.toLinkedString()));
ListNode midd = middleNode(head);
System.out.println("\t--中间元素:" + (midd == null ? "null" : midd.toLinkedString()));
}
public static void main(String args[]) {
new _006_876_链表的中间结点().test();
}
/*
组合完成:ListNode: 【1】
中间元素:ListNode: 【1】
组合完成:ListNode: 【1->2】
中间元素:ListNode: 【2】
组合完成:ListNode: 【1->2->3】
中间元素:ListNode: 【2->3】
组合完成:ListNode: 【1->2->3->4】
中间元素:ListNode: 【3->4】
组合完成:ListNode: 【1->2->3->4->5】
中间元素:ListNode: 【3->4->5】
组合完成:ListNode: 【1->2->3->4->5->6】
中间元素:ListNode: 【4->5->6】
组合完成:ListNode: 【1->2->3->4->5->6->7】
中间元素:ListNode: 【4->5->6->7】
组合完成:【null】
中间元素:null
*/
网友评论