Given a singly linked list L: L0→L1→…→Ln-1→Ln,
reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
You must do this in-place without altering the nodes' values.
For example,
reorder it to: L0→Ln→L1→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、将链表从中间分成两部分;
2、将后面部分链表反转;
3、将两个链表合起来;
AC代码:
public class ReorderList {
public void reorderList(ListNode head){
if( head == null || head.next == null ){
return;
}
//partition the list into two sublist of equal length
//sublist1 is equal or more 1 than sublist2
ListNode slowNode = head;
ListNode fastNode = head;
while( fastNode.next != null && fastNode.next.next != null ){
slowNode = slowNode.next;
fastNode = fastNode.next.next;
}
//sublist head
ListNode head1 = head;
ListNode head2 = slowNode.next;
//detach the list into two sublist
slowNode.next = null;
//2 reverse the second sublist, reverse the list when list's length bigger than 1
ListNode curr = head2;
ListNode post = curr.next;
curr.next = null;
while( post != null ){
ListNode temp = post.next;
post.next = curr;
curr = post;
post = temp;
}
head2 = curr;
//3 merge the two sublist
ListNode curr1 = head1;
ListNode curr2 = head2;
while( curr2 != null ){
ListNode post1 = curr1.next;
ListNode post2 = curr2.next;
curr1.next = curr2;
curr2.next = post1;
curr1 = post1;
curr2 = post2;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
测试用例:
import static org.junit.Assert.*;
import junit.framework.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class ReorderListTest {
private ReorderList reorder;
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testReorderList() {
reorder = new ReorderList();
ListNode list1 = new ListNode(1);
ListNode list2 = new ListNode(2);
ListNode list3 = new ListNode(3);
ListNode list4 = new ListNode(4);
list1.next = list2;
list2.next = list3;
list3.next = list4;
list4.next = null;
ReorderList reorder = new ReorderList();
reorder.reorderList(list1);
assertEquals(1, list1.val);
assertEquals(4, list1.next.val);
assertEquals(2, list1.next.next.val);
assertEquals(3, list1.next.next.next.val);
// System.out.println(list1.next.next.next.val);
// fail("Not yet implemented");
}
}
未AC,因为使用额外空间(自己记录):
// class ListNode{
// int val;
// ListNode next;
// ListNode(int x){
// val = x;
// next = null;
// }
// }
public class ReorderList {
public void reorderList(ListNode head){
if( head.next == null || head.next.next == null ){
return;
}
ListNode dummyHead = new ListNode(-1);
ListNode curr = dummyHead;
ListNode preEnd = head;
while( head.next != null && head.next.next != null ){
while( preEnd.next.next != null ){
preEnd = preEnd.next;
}
curr.next = head;
head = head.next;
curr = curr.next;
curr.next = preEnd.next;
curr = curr.next;
preEnd.next = null;
if( head.next == null || head.next.next == null){
curr.next = head;
break;
}
}
head = dummyHead.next;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ListNode list1 = new ListNode(4);
ListNode list2 = new ListNode(1);
ListNode list3 = new ListNode(3);
list1.next = list2;
list2.next = list3;
list3.next = null;
ListNode head = list1;
ReorderList reorder = new ReorderList();
reorder.reorderList(head);
System.out.println(head.next.val);
}
}