vector存储地址重排法
重排链表
http://www.nowcoder.com/questionTerminal/3d281dc0b3704347846a110bf561ef6b
在不改变链表内部值的情况下完成链表的重排,不知道我的方法是否符合题意,先将所有链表的地址从头到尾保存起来,然后用两个下标向中间靠拢重新排列,希望有大佬指正
class Solution { public: void reorderList(ListNode *head) { if (!head) return; vector<ListNode*> v; ListNode *tmp = head; ListNode *left, *right; int l, r; while (tmp != NULL) { v.push_back(tmp); tmp = tmp->next; } l = 0; r = v.size() - 1; tmp = head; while (l < r) { left = v[l++]; right = v[r--]; if (tmp == head) { tmp->next = right; tmp = tmp->next; } else { tmp->next = left; tmp = tmp->next; tmp->next = right; tmp = tmp->next; } } if (l == r) { tmp->next = v[l]; tmp = tmp->next; } tmp->next = NULL; } };