NC2. 重排链表
描述
示例1
输入:
{1,2,3,4}
输出:
{1,4,2,3}
说明:
给定head链表1->2->3->4, 重新排列为 1->4->2->3,会取head链表里面的值打印输出示例2
输入:
{1,2,3,4,5}
输出:
{1,5,2,4,3}
说明:
给定head链表1->2->3->4->5, 重新排列为 1->5>2->4->3,会取head链表里面的值打印输出示例3
输入:
{}
输出:
{}
C 解法, 执行用时: 4ms, 内存消耗: 1120KB, 提交时间: 2021-09-11
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ /** * * @param head ListNode类 * @return void */ struct ListNode* reserve(struct ListNode* head) { if( !head->next) { return head; } struct ListNode* p = head->next; head->next = NULL; while(p) { struct ListNode* temp = p; p = p->next; temp->next = head->next; head->next = temp; } return head; } void reorderList(struct ListNode* head ) { // write code here if(!head || !head->next || !head->next->next) { return ; } struct ListNode* slow = head; struct ListNode* fast = head; while(fast->next) { fast = fast->next; if(fast->next) { slow = slow->next; fast = fast->next; } } slow = reserve(slow); struct ListNode* newList = slow->next; slow->next = NULL; fast = head; while(fast && newList) { struct ListNode* temp1 = newList->next; struct ListNode* temp2 = fast->next; fast->next = newList; newList->next = temp2; fast = temp2; newList = temp1; } }
C 解法, 执行用时: 5ms, 内存消耗: 1148KB, 提交时间: 2021-09-08
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ /** * * @param head ListNode类 * @return void */ void reorderList(struct ListNode* head ) { // write code here struct ListNode *p = head, *q = p; struct ListNode *r = NULL, *s; if(head){ while(q->next){ q = q->next; if(q->next){ q = q->next; p = p->next; } else{ break; } } q = p->next;//找到第二个表 p->next = NULL; p = head; //逆置第二个表 while(q){ s = q->next; q->next = r; r = q; q = s; } //r指向第二个表的第一个节点 q = r; while(q){ r = r->next; s = p->next; p->next = q; q->next = s; p = s; q = r; } } }
C 解法, 执行用时: 5ms, 内存消耗: 1248KB, 提交时间: 2021-09-09
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ /** * * @param head ListNode类 * @return void */ void reorderList(struct ListNode* head) { if (head == NULL) { return; } struct ListNode* vec[40001]; struct ListNode* node = head; int n = 0; while (node != NULL) { vec[n++] = node; node = node->next; } int i = 0, j = n - 1; while (i < j) { vec[i]->next = vec[j]; i++; if (i == j) { break; } vec[j]->next = vec[i]; j--; } vec[i]->next = NULL; }
C 解法, 执行用时: 5ms, 内存消耗: 1336KB, 提交时间: 2021-11-18
/** * struct ListNode { * int val; * struct ListNode *next; * }; * * C语言声明定义全局变量请加上static,防止重复定义 */ /** * * @param head ListNode类 * @return void */ void reorderList(struct ListNode* head ) { // write code here if(head==NULL||head->next==NULL) return; struct ListNode* node =head; int lenth=0; int i=0,j=0; while(node!=NULL) { node=node->next; lenth++; } node=head; struct ListNode* arr[lenth]; while(node!=NULL) { arr[i++]=node; node=node->next; } i=0; j=lenth-1; while(i<j) { arr[i]->next=arr[j]; i++; if(i==j) break; arr[j]->next=arr[i]; j--; } arr[i]->next=NULL; }
C 解法, 执行用时: 5ms, 内存消耗: 1348KB, 提交时间: 2021-08-09
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ /** * * @param head ListNode类 * @return void */ void reorderList(struct ListNode* head ) { // write code here if (head == NULL) { return; } struct ListNode* vec[40001]; struct ListNode* node = head; int n = 0; while (node != NULL) { vec[n++] = node; node = node->next; } int i = 0, j = n - 1; while (i < j) { vec[i]->next = vec[j]; i++; if (i == j) { break; } vec[j]->next = vec[i]; j--; } vec[i]->next = NULL; }