解题思路:采用快慢指针的方法将单链表分成两半,再对两部分的链表进行合并
void reorderList(struct ListNode* head) { if (head == NULL || head->next == NULL) { return; } // 找到链表中间节点 struct ListNode* slow = head; struct ListNode* fast = head; while (fast != NULL && fast->next != NULL) { slow = slow->next; fast = fast->next->next; } // 翻转后半部分链表 struct ListNode* prev = NULL; struct ListNode* curr = slow; struct ListNode* nextTemp; while (curr != NULL) { nextTemp = curr->next; curr->next = prev; prev = curr; curr = nextTemp; } // 合并两个链表 struct ListNode* firstHalf = head; struct ListNode* secondHalf = prev; while (secondHalf->next != NULL) { struct ListNode* firstNextTemp = firstHalf->next; struct ListNode* secondNextTemp = secondHalf->next; firstHalf->next = secondHalf; secondHalf->next = firstNextTemp; firstHalf = firstNextTemp; secondHalf = secondNextTemp; } } 标签:ListNode,struct,next,链表,重排,curr,NULL From: https://www.cnblogs.com/ymy1/p/17694492.html