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

思路:

1、将链表从中间分成两部分;

2、将后面部分链表反转;

3、将两个链表合起来;

AC代码:

public class ReorderList {

	public void reorderList(ListNode head){
		if( head == null || head.next == null ){
			return;
		}
			
		//partition the list into two sublist of equal length
		//sublist1 is equal or more 1 than sublist2
		ListNode slowNode = head;
		ListNode fastNode = head;
		while( fastNode.next != null && fastNode.next.next != null ){
			slowNode = slowNode.next;
			fastNode = fastNode.next.next;
		}
		//sublist head
		ListNode head1 = head;
		ListNode head2 = slowNode.next;
		//detach the list into two sublist
		slowNode.next = null;
		
		//2 reverse the second sublist, reverse the list when list's length bigger than 1
		ListNode curr = head2;
		ListNode post = curr.next;
		curr.next = null;
		while( post != null ){
			ListNode temp = post.next;
			post.next = curr;
			curr = post;
			post = temp;
		}
		head2 = curr;
		//3 merge the two sublist
		ListNode curr1 = head1;
		ListNode curr2 = head2;
		while( curr2 != null ){
			ListNode post1 = curr1.next;
			ListNode post2 = curr2.next;
			curr1.next = curr2;
			curr2.next = post1;
			curr1 = post1;
			curr2 = post2;
		}
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
	}

}


测试用例:

import static org.junit.Assert.*;
import junit.framework.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class ReorderListTest {
	private ReorderList reorder;
	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testReorderList() {
		reorder = new ReorderList();
		ListNode list1 = new ListNode(1); 
		ListNode list2 = new ListNode(2);
		ListNode list3 = new ListNode(3);
		ListNode list4 = new ListNode(4);
		list1.next = list2;
		list2.next = list3;
		list3.next = list4;
		list4.next = null;
		ReorderList reorder = new ReorderList();
		reorder.reorderList(list1);
		assertEquals(1, list1.val);
		assertEquals(4, list1.next.val);
		assertEquals(2, list1.next.next.val);
		assertEquals(3, list1.next.next.next.val);
//		System.out.println(list1.next.next.next.val);
//		fail("Not yet implemented");
	}

}


未AC,因为使用额外空间(自己记录):

//	class ListNode{
//		int val;
//		ListNode next;
//		ListNode(int x){
//			val = x;
//			next = null;
//		}
//	}
public class ReorderList {

	public void reorderList(ListNode head){
		if( head.next == null || head.next.next == null ){
			return;
		}
		ListNode dummyHead = new ListNode(-1);
		ListNode curr = dummyHead;
		ListNode preEnd = head;
		while( head.next != null && head.next.next != null ){
			while( preEnd.next.next != null ){
				preEnd = preEnd.next;
			}
			curr.next = head;
			head = head.next;
			curr = curr.next;
			curr.next = preEnd.next;
			curr = curr.next;
			preEnd.next = null;
			if( head.next == null || head.next.next == null){
				curr.next = head;
				break;
			}
		}
		head = dummyHead.next;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ListNode list1 = new ListNode(4); 
		ListNode list2 = new ListNode(1);
		ListNode list3 = new ListNode(3);
		list1.next = list2;
		list2.next = list3;
		list3.next = null;
		ListNode head = list1;
		
		ReorderList reorder = new ReorderList();
		reorder.reorderList(head);
		System.out.println(head.next.val);
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值