c语言练习92:链表的中间结点
链表的中间结点
链表的结点为空时无法访问其next成员否则会报错
/*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*/typedef struct ListNode ListNode;
struct ListNode* middleNode(struct ListNode* head){if(head==NULL){return NULL;}ListNode*slow,*fast;fast=slow=head;while(fast&&fast->next){slow=slow->next;fast=fast->next->next;}
return slow;
}