您的位置:首页 > 其它

leetcode143~Reorder List

2017-03-24 10:03 561 查看
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}

思路: 1 利用俩指针找到中间节点,将链表分为两个部分

2 对后半部分的链表进行逆序

3 合并两个链表

public void reorderList(ListNode head) {
if(head==null || head.next==null) return;
ListNode fast = head;
ListNode slow = head;
while(fast.next!=null && fast.next.next!=null) {
slow = slow.next;
fast = fast.next.next;
}
//此时slow是中间的节点
ListNode rhead = slow.next;
slow.next = null;
rhead = reverse(rhead);

//合并链表
while(head!=null && rhead!=null) {
ListNode tmp1 = head.next;
ListNode tmp2 = rhead.next;
head.next = rhead;
rhead.next = tmp1;

head = tmp1;
rhead = tmp2;
}
}

//翻转
private ListNode reverse(ListNode head) {
ListNode pre = null;
while(head!=null) {
ListNode tmp = head.next;
head.next = pre;
pre = head;
head = tmp;
}
return pre;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: