Posts Tagged 【List 】Reorder List

Reorder List

  Total Accepted: 37888  Total Submissions: 180454 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.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
 
 /*
 我的思路比较简单,牺牲内存来处理,明天想想直接用指针来
 建立一个headnew,方便处理
 最后的next为null
 */
public class Solution {
    public void reorderList(ListNode head) {
        if(head == null) return;
        List<ListNode> list = new ArrayList<ListNode>();
        ListNode headNew = new ListNode(0);
        headNew.next = head;
        ListNode p = head;
        while(p!=null) {
            list.add(p);
            p = p.next;
        }
        int len = list.size();
        p = headNew;
        for(int i = 0;i<len/2;i++) {
            p.next = list.get(i);
            p.next.next = list.get(len-1-i);
            p = p.next.next;
        }
        p.next = list.get(len/2);
        p.next.next = null;
            
    }
}
</pre><pre code_snippet_id="651774" snippet_file_name="blog_20150423_1_2185049" name="code" class="java">/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
 
 /*
 接着来
 将list分成前后两个部分
 后一个部分反转
 两个部分插入
 注意将最后的node的next赋值为null
 */
public class Solution {
    public void reorderList(ListNode head) {
        if(head == null || head.next == null) return;
        twistList(head,reverseList(midList(head)));
    }
    
    public ListNode midList(ListNode head) {
        if(head == null || head.next == null) return null;
        ListNode p = head,q = head.next;
        while(q != null && q.next != null) {
            p = p.next;
            q = q.next.next;
        }
        return p.next;
    }
    
    public ListNode reverseList(ListNode head) {
        ListNode p=head,q=head.next;
        head.next = null;
        while(q != null) {
            ListNode temp = q.next;
            q.next = p;
            p = q;
            q = temp;
        }
        return p;
    }
    
    public ListNode twistList(ListNode head1,ListNode head2) {
        ListNode q = head1,p = head2;
        ListNode newHead = new ListNode(0);
        ListNode qp = newHead;
        while(q != null) {
            if(p != null) {
                ListNode qTemp = q.next;
                ListNode pTemp = p.next;
                qp.next = q;
                qp.next.next = p;
                qp = p;
                q = qTemp;
                p = pTemp;   
            } else {
                qp.next = q;
                qp.next.next = null;
                break;
            }
            
        }
        return newHead.next;
    }
}


 
     


Have you met this question in a real interview? 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值