# LeetCode -- Reorder List

Given a singly linked list L: L0→L1→…→Ln-1→Ln,
reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…

You must do this in-place without altering the nodes' values.

For example,
Given {1,2,3,4}, reorder it to {1,4,2,3}.

/**
* public class ListNode {
*     public int val;
*     public ListNode next;
*     public ListNode(int x) { val = x; }
* }
*/
public class Solution {
{
return;
}

while(q.next.next != null){
while(p.next.next != null){
p = p.next;
}

var t = q.next;
q.next = p.next;
// point last to 2nd and set the second last to null
p.next.next = t;

// point 2nd last to null
p.next = null;

// reset p and q
p = t;
q = t;
if(q.next == null){
break;
}
}
}
}


http://www.acmerblog.com/reorder-list-leetcode-6088.html

1.使用slow和fast指针将链表分为两部分，part1和part2 ，假设链表为1->2->3->4->5->6->7->8, part1 = {1->2->3->4} , part2= {5->6->7->8}
2.然后对part2逆置,即8->7->6->5
3.然后分别将part1[0]->part2[0], part2[0]->part1[1], part1[1]->part2[1]...

part1[i] -> part2[i]
part2[i] -> part1[i+1]
i++

/**
* public class ListNode {
*     public int val;
*     public ListNode next;
*     public ListNode(int x) { val = x; }
* }
*/
public class Solution {
{
return;
}

while(fast.next != null && fast.next.next != null)
{
slow = slow.next;
fast = fast.next.next;
}

var mid = slow.next;
var last = mid;
ListNode pre = null;
while(last != null){
ListNode next = last.next;
last.next = pre;
pre = last;
last = next;
}
slow.next = null;

while(head != null && pre != null)
{
pre = pre.next;
}
}
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：LeetCode -- Reorder List 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)