[算法面试题]-重新排序链表(Java)
题目
给定一个简单的链表L:L0→L1→...→Ln-1→Ln,将其
重新排序为:L0→Ln→L1→Ln-1→L2→Ln-2→...
例如,给定{1,2,3,4},将其重新排序为{1,4,2,3}。您必须在原有链表上操作,而不能创建新的链表。
参考解决方案
由于该问题需要在原有链表上操作,因此我们只能更改其指针,而不能创建新链表。通过执行以下三个步骤可以解决此问题:
中间列表到两个列表的中断列表(使用快速和慢速指针)
颠倒第二个列表的顺序
将两个列表合并在一起
以下代码是带有测试的完整可运行类。
//Class definition of ListNode
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
public class ReorderList {
public static void main(String[] args) {
ListNode n1 = new ListNode(1);
ListNode n2 = new ListNode(2);
ListNode n3 = new ListNode(3);
ListNode n4 = new ListNode(4);
n1.next = n2;
n2.next = n3;
n3.next = n4;
printList(n1);
reorderList(n1);
printList(n1);
}
public static void reorderList(ListNode head) {
if (head != null && head.next != null) {
ListNode slow = head;
ListNode fast = head;
//use a fast and slow pointer to break the link to two parts.
while (fast != null && fast.next != null && fast.next.next!= null) {
//why need third/second condition?
System.out.println("pre "+slow.val + " " + fast.val);
slow = slow.next;
fast = fast.next.next;
System.out.println("after " + slow.val + " " + fast.val);
}
ListNode second = slow.next;
slow.next = null;// need to close first part
// now should have two lists: head and fast
// reverse order for second part
second = reverseOrder(second);
ListNode p1 = head;
ListNode p2 = second;
//merge two lists here
while (p2 != null) {
ListNode temp1 = p1.next;
ListNode temp2 = p2.next;
p1.next = p2;
p2.next = temp1;
p1 = temp1;
p2 = temp2;
}
}
}
public static ListNode reverseOrder(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode pre = head;
ListNode curr = head.next;
while (curr != null) {
ListNode temp = curr.next;
curr.next = pre;
pre = curr;
curr = temp;
}
// set head node's next
head.next = null;
return pre;
}
public static void printList(ListNode n) {
System.out.println("------");
while (n != null) {
System.out.print(n.val);
n = n.next;
}
System.out.println();
}
}