[LeetCode] 143。重新排序列表

  

给定一个单链list  L : , L L →…→L ,
重新排序为:L
, L L L L L →…

你必须就地在不改变节点的值。例如,


Given  <代码>{1,2,3,4}> {1 4 2 3}>

L L L L L

/* *   ,* Definition  for  singly-linked 列表。   ,* struct  ListNode  {   ,*,,,,int  val;   ,*,,,,struct  ListNode  *下;   ,*};   ,*/void  reorderList (struct  ListNode *,头)   {   ,,,if  (==,, head  NULL  | |,头→next ==, NULL  | |,头→下一步→next ==, NULL )   ,,,{   ,,,,,,,返回;   ,,,}   ,,,,   ,,,struct  ListNode  * list ,=,头;   ,,,int  len =, 0;   ,,,while  (, list )   ,,,{   ,,,,,,,list =,列表→下;   ,,,,,,,len  +=, 1;   ,,,}   ,,,,   ,,,int  key =, 0;   ,,,if  (%, len  2,,==, 0,)   ,,,{   ,,,,,,,key =, len /, 2;   ,,,}   ,,,   ,,,{   ,,,,,,,key =,(时间/len  2), +, 1;   ,,,}   ,,,,   ,,,struct  ListNode  * second =,头;   ,,,list ,=,头;   ,,,int  cnt =, 0;   ,,,while  (, cnt  & lt;, key )   ,,,{   ,,,,,,,list =,第二;   ,,,,,,,second =,第二→下;   ,,,,,,,cnt  +=, 1;   ,,,}   ,,,列表→next =,空;   ,,,,   ,,,list =,空;   ,,,struct  ListNode  * next =,空;   ,,,while  (, second )   ,,,{   ,,,,,,,next =,第二→下;   ,,,,,,,第二个→next =,列表;   ,,,,,,,list =,第二;   ,,,,,,,second =,下一个;   ,,,}   ,,,second =,列表;   ,,,,   ,,,next =,空;   ,,,while  (, second )   ,,,{   ,,,,,,,next =,第二;   ,,,,,,,second =,第二→下;   ,,,,,,,下一个→next =,头→下;   ,,,,,,,头→next =,下一个;   ,,,,,,,head =,头→下一步→下;   ,,,}   }


[LeetCode] 143。重新排序列表