您的位置:首页 > 编程语言

2014年微软校园招聘笔试题最后编程题Reorder List

2013-12-17 12:58 459 查看

1.问题

Given a singly linked list L: (L0 , L1 , L2...Ln-1 , Ln). Write a program to reorder it so that it becomes(L0 , Ln , L1 , Ln-1 , L2 , Ln-2...).

struct Node  

{  

    int val_;  

    Node* next;  

};  

Notes:

1)Space Complexity should be O(1) 

2)Only the ".next" field of a node is modifiable.

该题目和LeetCode中的Reorder List 题目完全一样:

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}.

2.分析

如果采用空间复杂度为O(n)和时间复杂度为O(1)的方法解决Reorder List问题,主要分为以下三步:

1)将单链表分为前后两个链表;

2)逆序分开后的第二个链表;

3)再将前后链表合并成一个单链表。

3.程序实现

/*
*@题目描述: 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.将两个链表合并。
*/
//链表的数据结构
class ListNode{
ListNode next;
int val;
public ListNode(int val) {
// TODO Auto-generated constructor stub
this.val=val;
next=null;
}
}

public class ReorderList {
public static ListNode reverseList(ListNode head){
if(head==null || head.next==null)
return head;
ListNode preNode=head;
ListNode currNode=head.next;
while(currNode!=null){
ListNode tempNode=currNode.next;
currNode.next=preNode;
preNode=currNode;
currNode=tempNode;
}
head.next=null;
return preNode;
}
public static void  reorderList(ListNode head){
if(head!=null && head.next!=null){
//将两个链表从中间分为两个链表
ListNode slowNode=head;
ListNode fastNode=head;
while(fastNode!=null&&fastNode.next!=null&&fastNode.next.next!=null){
slowNode=slowNode.next;
fastNode=fastNode.next.next;
}
ListNode secondListHead=slowNode.next;
slowNode.next=null;
//逆转第二个链表
secondListHead=reverseList(secondListHead);
ListNode pHead1=head;
ListNode pHead2=secondListHead;
//合并两个分开的链表
while(pHead2!=null){
ListNode pTempNode1=pHead1.next;
ListNode pTempNode2=pHead2.next;
pHead1.next=pHead2;
pHead2.next=pTempNode1;
pHead1=pTempNode1;
pHead2=pTempNode2;
}
}
}
public static void printListInfo(ListNode head){
System.out.println("*****开始分隔符*****");
while(head!=null){
System.out.print(head.val);
head=head.next;
}
System.out.println("\n*****结束分隔符*****");
}
public static void main(String args[]){
ListNode listHead=new ListNode(3);
ListNode listNode1=new ListNode(4);
ListNode listNode2=new ListNode(5);
ListNode listNode3=new ListNode(6);
ListNode listNode4=new ListNode(7);
listHead.next=listNode1;
listNode1.next=listNode2;
listNode2.next=listNode3;
listNode3.next=listNode4;
System.out.println("Before Reorder list: ");
printListInfo(listHead);
reorderList(listHead);
System.out.println("after Reorder List:");
printListInfo(listHead);
}
}


运行结果如下:
Before Reorder list: 

*****开始分隔符*****

34567

*****结束分隔符*****

after Reorder List:

*****开始分隔符*****

37465

*****结束分隔符*****


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐