学习目标:
力扣第143题:重排链表
学习内容:
给定一个单链表 L:L0→L1→…→Ln-1→Ln ,
将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→…
你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。
示例 1:
给定链表 1->2->3->4, 重新排列为 1->4->2->3.
示例 2:
给定链表 1->2->3->4->5, 重新排列为 1->5->2->4->3.
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reorder-list
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
学习时间:
2020年10月26日
学习产出:
/*
*
public void reorderList(ListNode head) {
if (head == null) {
return;
}
List<ListNode> list = new ArrayList<ListNode>();
ListNode h = head;
while (h != null) {
list.add(h);
h = h.next;
}
int l = 0, r = list.size() - 1;
while (l < r) {
list.get(l).next = list.get(r);
l++;
if (l == r) {
break;
}
list.get(r).next = list.get(l);
r--;
}
list.get(l).next = null;
}