Created
February 18, 2020 03:56
-
-
Save shaobin0604/385da38c7fb4965dcffea3cdd09b4ddf to your computer and use it in GitHub Desktop.
链表快慢指针找中间节点
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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