Skip to content

Instantly share code, notes, and snippets.

@shaobin0604
Created February 18, 2020 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shaobin0604/385da38c7fb4965dcffea3cdd09b4ddf to your computer and use it in GitHub Desktop.
Save shaobin0604/385da38c7fb4965dcffea3cdd09b4ddf to your computer and use it in GitHub Desktop.
链表快慢指针找中间节点
ListNode slow = head;
ListNode fast = head.next;
// 奇数链表,slow 位于中间节点
// 偶数链表,slow 位于左边节点
while (slow.next != null && fast.next != null) {
slow = slow.next;
fast = fast.next;
}
// 后半部分头节点
ListNode tmp = slow.next;
// 前半部分尾节点断开与后半部分的连接
slow.next = null;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment