leetcode 876. middle of the linked list



  1. Middle of the Linked List:题目链接
原理

AC code:

1
2
3
4
5
6
7
8
9
10
11
12
struct ListNode* (struct ListNode* head){
if (head == NULL || head->next == NULL) {
return head;
}
struct ListNode* fast = head, * slow = head;

while (fast!=NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}

如果是返回第一个中间节点呢?

原理