Note:
笨方法,开个Hash表把每一个节点的前面的指针给存下来,然后每次从后面扣一个给插到中间位置
代码如下:
class Solution {
public:
void reorderList(ListNode* head) {
if(!head || !head -> next)
return;
unordered_map<ListNode*, ListNode*> hash;
auto p = head, q = head -> next;
for(p = head, q = head -> next; q; p = p -> next, q = q -> next)
hash[q] = p;
auto last = p;
for(p = head; p && p != last; p = p -> next -> next){
hash[last] -> next = NULL;
last -> next = p -> next;
p -> next = last;
last = hash[last];
}
}
};
题解方法:
1、开个数组重新记录一下子,然后再重新构建链表
2、先找到链表的中点,然后给后面的链表翻转一下,再进行合并
有点麻烦,二刷的时候在写这个办法吧