categories: [Blog,Algorithm]


876. 链表的中间结点

有多个返回第二个

  1. public ListNode middleNode(ListNode head) {
  2. ListNode[] A = new ListNode[100];
  3. int t = 0;
  4. while (head != null) {
  5. A[t++] = head;
  6. head = head.next;
  7. }
  8. return A[t / 2];
  9. }
  10. 作者:LeetCode-Solution
  11. 链接:https://leetcode-cn.com/problems/middle-of-the-linked-list/solution/lian-biao-de-zhong-jian-jie-dian-by-leetcode-solut/

  1. public ListNode middleNode(ListNode head) {
  2. ListNode slow = head, fast = head;
  3. while (fast != null && fast.next != null) {
  4. slow = slow.next;
  5. fast = fast.next.next;
  6. }
  7. return slow;
  8. }