题解 | #重排链表#
重排链表
http://www.nowcoder.com/practice/3d281dc0b3704347846a110bf561ef6b
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public void reorderList(ListNode head) {
if(head == null) return;
if(head.next == null) return;
if(head.next.next == null) return;
ListNode second = head.next;
ListNode tail = head.next.next;
ListNode pre = head.next;
while(tail.next != null){
pre = tail;
tail = tail.next;
}
pre.next = null;
head.next = tail;
tail.next = second;
reorderList(second);
}
}