/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
void reorderList(struct ListNode* head){
struct ListNode **result = (struct ListNode **)malloc(sizeof(struct ListNode *) * 50000);
memset(result, 0, sizeof(struct ListNode *) * 50000);
int i = 0, j=0;
while(head)
{
result[i++] = head;
head = head->next;
}
head = result[0];
for(j=i-1,i=1; i<j; j--,i++)
{
head->next = result[j];
head = head->next;
head->next = result[i];
head = head->next;
}
if(i==j)
{
head->next = result[i];
head = head->next;
}
head->next = NULL;
head = result[0];
free(result);
}
|