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* middleNode(struct ListNode* head){
if (head == NULL || head->next == NULL) {
return head;
}
struct ListNode* fast = head, * slow = head;
// fast->next != NULL && fast->next->next != NULL 返回的是第一个中间节点
while (fast!=NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}


如果是返回第一个中间节点呢?
原理
文章目录
|