143. Reorder List

Given a singly linked list LL0→L1→…→Ln-1→Ln,
reorder it to: L0→LnL1→Ln-1→L2→Ln-2→…

You must do this in-place without altering the nodes' values.

For example,
Given {1,2,3,4}, reorder it to {1,4,2,3}.

含义:按照指定规则,重排列列表

 1         if (head == null || head.next == null) return;
 2         ListNode walker = head, runner = head;
 3         while (runner != null && runner.next != null) {
 4             walker = walker.next;
 5             runner = runner.next.next;
 6         }
 7         walker = walker.next;
 8         Stack<Integer> values = new Stack<>();
 9         while (walker != null) {
10             values.push(walker.val);
11             walker = walker.next;
12         }
13         ListNode p = head;
14         while (!values.isEmpty()) {
15             ListNode newNode = new ListNode(values.pop());
16             newNode.next = p.next;
17             p.next = newNode;
18             p = p.next.next;
19         }
20         if (runner == null) {
21 //            isEven = true;
22             p.next.next = null;
23         } else p.next = null;
原文地址:https://www.cnblogs.com/wzj4858/p/7729265.html