题目描述
给定一个头结点为 head 的非空单链表,返回链表的中间结点。
如果有两个中间结点,则返回第二个中间结点。
题解
快慢指针法: 双指针
用两个指针 slow 与 fast 一起遍历链表。slow 一次走一步,fast 一次走两步。那么当 fast 到达链表的末尾时,slow 必然位于中间。
画图思考细节
本题要求如果有两个中间结点,则返回第二个中间结点。
此时快指针可以前进的条件是:当前快指针的结点和当前快指针的下一个结点都非空。
- 当节点数为奇数时, slow指针移动到链表中间位置;
- 当节点数为偶数时,slow指针移动到链表中间靠右位置;
若要求改为如果有两个中间结点,则返回第一个中间结点。
此时快指针可以前进的条件是:当前快指针的下一个结点和当前快指针的下下一个结点都非空。
- 当节点数为奇数时, slow指针移动到链表中间位置;
- 当节点数为偶数时,slow指针移动到链表中间靠左位置;
class Solution {
public:
ListNode* middleNode(ListNode* head) {
ListNode* slow = head;
ListNode* fast = head;
while (fast != NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
};
复杂度分析
- 时间复杂度:O(N),其中 N 是给定链表的结点数目。
- 空间复杂度:O(1),只需要常数空间存放 slow 和 fast 两个指针。
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def middleNode(self, head: ListNode) -> ListNode:
if head is None:
return None
slow = head
fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
def create_linked_list(nums):
if len(nums) == 0:
return None
head = ListNode(nums[0])
cur = head
for i in range(1, len(nums)):
cur.next = ListNode(nums[i])
cur = cur.next
return head
def print_linked_list(list_node):
if list_node is None:
return
cur = list_node
while cur:
print(cur.val, '->', end=' ')
cur = cur.next
print('null')
if __name__ == '__main__':
# nums = [1, 2, 3, 4, 5, 6, 7]
nums = [1, 2, 3, 4, 5, 6, 7, 8]
head = create_linked_list(nums)
solution = Solution()
result = solution.middleNode(head)
print('结果:')
print_linked_list(result)