Problem 143: Reorder List
思路
链表的题就是基本功的题目。稍微复杂的题目其实就是拆分成几个基本的操作而已。
先找中点
reverse后半部分
合并左右
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public void reorderList(ListNode head) {
if (head == null || head.next == null) {
return;
}
ListNode mid = findMiddle(head);
ListNode right = reverse(mid.next);
mid.next = null;
merge(head, right);
return;
}
private ListNode findMiddle(ListNode head) {
ListNode slow = head;
ListNode fast = head.next;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
private void merge(ListNode head1, ListNode head2) {
int index = 0;
ListNode dummy = new ListNode(0);
while (head1 != null && head2 != null) {
if (index % 2 == 0) {
dummy.next = head1;
head1 = head1.next;
} else {
dummy.next = head2;
head2 = head2.next;
}
dummy = dummy.next;
index++;
}
if (head1 != null) {
dummy.next = head1;
}
if (head2 != null) {
dummy.next = head2;
}
}
private ListNode reverse(ListNode head) {
ListNode prev = null;
while (head != null) {
ListNode temp = head.next;
head.next = prev;
prev = head;
head = temp;
}
return prev;
}
}
易错点
reverse链表一定要熟练
private ListNode reverse(ListNode head) { ListNode prev = null; while (head != null) { ListNode temp = head.next; head.next = prev; prev = head; head = temp; } return prev; }
最后返回的是prev,而不是next。
学会用index来控制merge
另外不要忘了更新index:index++
函数返回值是void类型
Last updated