题目:
示例:
思路:
这个题类似于寻找链表中间的数字,slow和fast都指向head,slow走一步,fast走两步,也许你会有疑问,节点数的奇偶不考虑吗?while执行条件写成fast&&fast->next就OK,不理解可以画个图,自己举个例子就能看懂了。
代码:
struct ListNode* deleteMiddle(struct ListNode* head) { if(head == NULL || head->next == NULL) return NULL; struct ListNode* prev = NULL; struct ListNode* slow = head; struct ListNode* fast = head; while(fast && fast->next) { prev = slow; slow = slow->next; fast = fast->next->next; } struct ListNode* next = slow->next; prev->next = next; free(slow); return head; }