143.重排链表
class Solution {
public void reorderList(ListNode head) {
if(head == null){
return;
}
ListNode mid = middleNode(head);
ListNode l1 = head;
ListNode l2 = mid.next;
mid.next = null;
l2 = reverseList(l2);
mergeList(l1,l2);
}
public ListNode middleNode(ListNode head){
ListNode slow = head;
ListNode fast = head;
while(fast.next != null && fast.next.next != null){
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
public ListNode reverseList(ListNode head){
ListNode pre = null;
ListNode cur = head;
while(cur!= null){
ListNode next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
public void mergeList(ListNode L1,ListNode L2){
ListNode l1_tmp;
ListNode l2_tmp;
while(L1!=null && L2 != null){
l1_tmp = L1.next;
l2_tmp = L2.next;
L1.next = L2;
L1 = l1_tmp;
L2.next = L1;
L2 = l2_tmp;
}
}
}