标签:
题目描述:/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution {
public void ReorderList(ListNode head)
{
if(head == null || head.next == null || head.next.next == null){
return;
}
var p = head;
var q = head;
while(q.next.next != null){
while(p.next.next != null){
p = p.next;
}
// point head to last
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;
}
}
}
}
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution {
public void ReorderList(ListNode head)
{
if(head == null || head.next == null || head.next.next == null){
return;
}
var slow = head;
var fast = head;
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)
{
var next1 = head.next;
head.next = pre;
pre = pre.next;
head.next.next = next1;
head = next1;
}
}
}标签:
原文地址:http://blog.csdn.net/lan_liang/article/details/49962425