一、题目内容
二、题解
解法1:
思路
快慢指针找中点后逆序后半部分,拼接链表
代码
public class Solution { public void reorderList(ListNode head) { if(head == null||head.next == null||head.next.next == null){ return; } ListNode fast = head,slow = head; while(fast!=null && fast.next!=null){ fast = fast.next.next; slow = slow.next; } ListNode next = slow.next; slow.next = null; ListNode newStart = reverse(next); slow = head; while(slow!=null&&newStart!=null){ ListNode slowNext = slow.next; slow.next = null; ListNode newStartNext = newStart.next; newStart.next = null; slow.next = newStart; newStart.next = slowNext; slow = slow.next.next; newStart = newStartNext; } } private ListNode reverse(ListNode root){ if(root.next == null){ return root; } ListNode pre = null; ListNode cur = root; while(cur!=null){ ListNode next = cur.next; cur.next = pre; pre = cur; cur = next; } return pre; }}