1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
public class ReorderList { public void reorderList(ListNode head) { if (head == null || head.next == null || head.next.next == null) { return; } List<ListNode> list = new ArrayList<ListNode>(); list.add(head); ListNode cursor = head.next; int count = 1; while (cursor != null) { list.add(cursor); cursor = cursor.next; count++; } for (int i = 0; i < count / 2; i++) { list.get(i).next = list.get(count - i - 1); list.get(count - i - 1).next = list.get(i + 1); } list.get(count / 2).next = null; } }
|