题解 | #重排链表#
重排链表
http://www.nowcoder.com/practice/3d281dc0b3704347846a110bf561ef6b
1、双指针算法
import java.util.*; public class Solution { public void reorderList(ListNode head) { if (head == null || head.next == null) return; List<ListNode> list = new ArrayList<>(); ListNode cur = head; while (cur != null) { list.add(cur); cur = cur.next; } int l = 0, r = list.size() - 1; while (l < r) { list.get(l).next = list.get(r); l++; list.get(r).next = list.get(l); r--; } list.get(l).next = null; } }