Leetcode-Reorder List

作者:disappearedgod
时间:2014-8-20

题目

Reorder List

  Total Accepted: 9237  Total Submissions: 47615 My Submissions

Given a singly linked list LL0L1→…→Ln-1Ln,
reorder it to: L0LnL1Ln-1L2Ln-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);
    }


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值