作者:disappearedgod
时间:2014-8-20
题目
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,
Given {1,2,3,4}
, reorder it to {1,4,2,3}
.
Definition for singly-linked list.
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public void reorderList(ListNode head) {
if(head == null || head.next == null)
return;
ListNode p = head;
ListNode reversal = new ListNode(head.val);
ListNode rp = new ListNode(head.val);//这里为了形成一个环 所以先new一个点,而不是null,在for里面形成这个点
ListNode tmp = null;
int length = 0;
for(p = head.next; p !=null; p = p.next){
tmp = new ListNode(p.val);
tmp.next = rp;
rp = tmp;//这两句形成环 如果p = head 的情况下
length++;
}
reversal = rp;
//printList(reversal);
p = head;
rp = reversal;
head = rp;
ListNode t_pN = null;
ListNode t_rpN = null;
for(int i = 0; i < length/2; i++){
t_pN = p.next;
t_rpN = rp.next;
p.next = rp;
rp.next = t_pN;
p = t_pN;
rp = t_rpN;
}
if(p != null)
if(length%2!=0 && p.next !=null)
p.next.next = null;
else
p.next = null;
}
}
调试文件
package List;
/**
* Created by yuyan on 14-8-20.
*/
public class ReorderList extends ListTest {
@Override
protected void work(ListNode head) {
super.work(head);
if(head == null || head.next == null)
return;
ListNode p = head;
ListNode reversal = new ListNode(head.val);
ListNode rp = new ListNode(head.val);
ListNode tmp = null;
int length = 0;
for(p = head.next; p !=null; p = p.next){
tmp = new ListNode(p.val);
tmp.next = rp;
rp = tmp;
length++;
}
reversal = rp;
p = head;
rp = reversal;
ListNode t_pN = null;
ListNode t_rpN = null;
for(int i = 0; i < length/2; i++){
t_pN = p.next;
t_rpN = rp.next;
p.next = rp;
rp.next = t_pN;
p = t_pN;
rp = t_rpN;
}
if(p != null)
if(length%2!=0 && p.next !=null)
p.next.next = null;
else
p.next = null;
}
public static void main(String[] args){
ReorderList test = new ReorderList();
int[] a = {1,2,3};
ListNode head = test.buildTestListFromArray(a);
//test.printList(head);
test.work(head);
test.printList(head);
}
}
package List;
/**
* Created by yuyan on 14-8-20.
*/
public class ListTest {
protected void work(ListNode head){
}
protected void printList(ListNode head){
if(head == null){
System.out.print("[ ]");
return;
}
for(; head.next !=null; head = head.next){
System.out.print("["+ head.val+"]->");
}
System.out.print("["+ head.val+"]");
}
protected ListNode buildTestListFromArray(int[] a){
ListNode testList = new ListNode(a[0]);
ListNode p = testList;
for(int i = 1 ; i < a.length; i++){
ListNode tmp = new ListNode(a[i]);
p.next = tmp;
p = p.next;
}
return testList;
}
protected ListNode buildDescendTestList(int N){
ListNode testList = null;
for(int i = 0 ; i < N; i++){
ListNode tmp = new ListNode(i+1);
tmp.next = testList;
testList = tmp;
}
return testList;
}
protected ListNode buildAscendTestList(int N){
ListNode testList = new ListNode(1);
ListNode p = testList;
for(int i = 1 ; i < N; i++){
ListNode tmp = new ListNode(i+1);
p.next = tmp;
p = p.next;
}
return testList;
}
public static void main(String[] args){
ListTest t = new ListTest();
ListNode testList = t.buildAscendTestList(10);
t.printList(testList);
}
}