Leetcode 143: Reorder List

143. Reorder List

Given a singly linked list LL0→L1→…→Ln-1→Ln,
reorder it to: L0→LnL1→Ln-1→L2→Ln-2→…

You may not modify the values in the list's nodes, only nodes itself may be changed.

Example 1:

Given 1->2->3->4, reorder it to 1->4->2->3.

Example 2:

Given 1->2->3->4->5, reorder it to 1->5->2->4->3.

解法一:

考虑用双指针策略。p1用于记录L0,L1...Ln-1的位置,p2用于记录Ln,Ln-1...Ln-2的位置。所以这道题的大体思路就是通过p1寻找到相对应的p2的位置,然后将p2指向的node挪到p1.next,然后重复以上步骤。

那p2的具体位置怎么确定下来?直观的说,p2就是每次node换位后当前链表的尾节点。那找个尾节点又怎么找?难道每次节点换位后要把被移动节点的父节点的next置为null?不过这样一来就会增加程序的复杂性。所以我们可以用计算的方法计算出当前与p1相对应的尾节点p2,距离p1有多远。然后将p2移动到那个节点上做移动即可。

public static void reorderList(ListNode head) {
		ListNode p1 = head ;
		ListNode p2 = p1 ;
		int len = 0;
		
		while(p1 != null) {
			len ++ ;
			p1 = p1.next ;
		}
		
		p1 = head ;
		int step = len - 1 ;
		while(step >= 2) {
			for(int i=0; i<step; i++) { // move i steps to get n-i node 
				p2 = p2.next ;
			}
			p2.next = p1.next ;
			p1.next = p2 ;
			p1 = p2.next ;
			p2 = p1 ;
			step = step - 2 ;
		}
		
		if(step==0) {
			p1.next = null ;
		} else if(step == 1) {
			p1.next.next = null ;
		}
		
	}

解法二:

虽然解法一可以解答本题,但是效率上却不高。因为时间复杂度是O(N^2)的。细看程序不难发现,每次寻找p2位置的过程存在重复。如果我们已经知道了p1的位置i,那么p2的位置其实就是N-i。那么我们用简单的HashMap就能解决问题。Key是node节点在整个链表中的索引位(为了方便,从1开始就行),value就是对应索引位上的node节点的引用。那么程序如下:

	public static void reorderList(ListNode head) {
		if(head == null || head.next == null) {
			return ;
		}
		ListNode p1 = head ;
		ListNode p2 = p1 ;
		int len = 0;
		Map<Integer, ListNode> map = new HashMap<>() ;
		
		while(p1 != null) {
			len ++ ;
			map.put(len, p1) ;
			p1 = p1.next ;
		}
		
		int move = len ;
		p1 = head ;
		while(move >= len/2 + 1) {
			p2 = map.get(move) ;
			p2.next = p1.next ;
			p1.next = p2;
			p1 = p2.next ;
			move -- ;
		}
		
		p1.next = null ;
		
		
	}

解法三:

这样跑下来后会发现,虽然时间复杂度降到了O(N),但是依然没能超过其他网友给出的方案。莫非还有更好的解决方法?

这个方法就是先把整条链表均分成两段,把后面一段的所有元素倒置,然后将两条链表逐位merge在一起就行了。

// Copy from: https://leetcode.com/problems/reorder-list/discuss/451646/100-both
public static void reorderList(ListNode head) {
		if(head==null)
            return;
		
        // Find the middle node
        ListNode run = head;
        ListNode walker = head;
        while(run!=null && run.next!=null){
            run = run.next.next;
            walker = walker.next;
        }
        //Break original list into 2 linkedist
        ListNode reverse = walker.next;
        walker.next=null;
        
        // Reverse the second linkedList
        ListNode l1 = head;
        ListNode prev = null;
        while(reverse!=null){
            ListNode temp = reverse;
            reverse = reverse.next;
            temp.next = prev;
            prev = temp;
        }
        ListNode l2 = prev;
        head = l1;
        //Merge 2 linkedList
        while(l2!=null){
            ListNode tmpl1 = l1;
            ListNode templ2 = l2;
            l1= l1.next;
            l2 = l2.next;
            tmpl1.next = templ2;
            templ2.next=l1;
        }
		
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yexianyi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值