您的位置:首页 > 其它

【LeetCode】Reorder List

2014-12-09 14:57 417 查看
题目

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}
.
解答
先将链表从中间截成两部分(奇数的前半部分多一,注意截断后最后个元素指向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 fast=head;
ListNode slow=head;
while(fast!=null&&fast.next!=null&&fast.next.next!=null){ //要获取中间节点的前一个元素
fast=fast.next.next;
slow=slow.next;
}
ListNode halfNode=slow.next;
slow.next=null; //注意此处要将前半段最后一个元素指向null
ListNode halfHead=null;
while(halfNode!=null){
ListNode pre=halfNode;
halfNode=halfNode.next;
pre.next=halfHead;
halfHead=pre;
}
ListNode cur=head;
while(halfHead!=null){
ListNode pre=halfHead;
halfHead=halfHead.next;
pre.next=cur.next;
cur.next=pre;
cur=cur.next.next; //注意指针要指向隔一个节点
}
}
}
---EOF---
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: