首页 > 其他 > 详细

Reorder List

时间:2016-08-26 13:34:35      阅读:122      评论:0      收藏:0      [点我收藏+]

开始用递归总是说超时,迫不得已改成这个了。

还是得注意细节

void reorderList(ListNode* head) {
if(head == NULL || head->next==NULL || head->next->next==NULL) return;
int i=1,j;
ListNode* p = head;
while(p->next) {
i++;
p = p->next;
}
j = i;
i = (i+1)/2;
ListNode* head2 = head;
for(int n=i; n>1; n--) {
head2 = head2->next;
}
ListNode* tail = head2;
head2 = head2->next;
tail->next = NULL;
p->next = head2;
head2 = head2->next;
p->next->next = NULL;
while(head2 != p) {
ListNode* now = head2;
head2 = head2->next;
now->next = p->next;
p->next = now;
}

p = head;
int n = j-i;
while(n--) {
ListNode* now = head2;
head2 = head2->next;
now->next = p->next;
p->next = now;
p = now->next;
}
head;
return;
}

Reorder List

原文:http://www.cnblogs.com/xdlyy/p/5809855.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!