Reorder List Time : O(n) Space : O(1) 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ListNode* reverse(ListNode* A) { ListNode* dummy = new ListNode(-1); ListNode* runner = A; while(runner) { ListNode* next = runner->next; runner->next = dummy->next; dummy->next = runner; runner = next; } return dummy->next;}ListNode* Solution::reorderList(ListNode* A) { if(A->next == NULL or A->next->next == NULL) return A; ListNode* slow = A; ListNode* fast = A; while(fast->next and fast->next->next) { slow = slow->next; fast = fast->next->next; } ListNode* back = slow->next; slow->next = NULL; ListNode* rev = reverse(back); ListNode* dummy = new ListNode(-1); ListNode* runner = dummy; while(A and rev) { if(A) { runner->next = A; A = A->next; runner = runner->next; runner->next = NULL; } if(rev) { runner->next = rev; rev = rev->next; runner = runner->next; runner->next = NULL; } } if(A) { runner->next = A; A = A->next; runner = runner->next; runner->next = NULL; } return dummy->next;}