LeetCode Solutions : 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 itto {1,4,2,3}.

Considering the following steps:

 * 1. split such list into two list, first and second, according to slow and fast point

* 2. reverse the second list

* 3. insert the second list into the first list

coding solution:

/**
 * 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){
			ListNode low=head;//1. split such list into two list, first and second, according to slow and fast point
			ListNode fast=head;
			while(fast.next!=null&&fast.next.next!=null){
				low=low.next;
				fast=fast.next.next;
			}
			ListNode first=head;
			ListNode second=low.next;
			low.next=null;

		    second=reverse(second);//2. reverse the second list
			while(second!=null){//3. insert the second list into the first list
				ListNode p1=first.next;
				ListNode p2=second.next;
				first.next=second;
				second.next=p1;
				first=p1;
				second=p2;
			}

		}
    }
	private ListNode reverse(ListNode head){
		if(head==null||head.next==null)
			return head;
		ListNode pre=head;
		ListNode cur=head.next;
		while(cur!=null){
			ListNode nextNode=cur.next;
			cur.next=pre;
			pre=cur;
			cur=nextNode;
		}
		head.next=null;
		return pre;
	}
}
时间: 2024-10-10 17:40:10

LeetCode Solutions : Reorder List的相关文章

LeetCode Solutions : Search in Rotated Sorted Array II

Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. Java Solution ( refer to my blog LeetCode So

【Leetcode】Reorder List JAVA

一.题目描述 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.暴力解法 这种解法所需的时间的时间复杂度比较高,为O(n2) 代码如下

[LeetCode] String Reorder Distance Apart

Question: Given a string of lowercase characters, reorder them such that the same characters are at least distance d from each other. Input: { a, b, b }, distance = 2 Output: { b, a, b } http://leetcode.com/2010/05/here-is-another-google-phone-interv

[LeetCode] 937. Reorder Data in Log Files 日志文件的重新排序

You have an array of?`logs`.? Each log is a space delimited string of words. For each log, the first word in each log is an alphanumeric?identifier.? Then, either: Each word after the identifier will consist only of lowercase letters, or; Each word a

leetcode 【 Reorder List 】python 实现

题目: 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}. 代码: oj 测试通过 248 ms 1 # Definition for singly-

[Leetcode][JAVA] 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}. 比较容易思考且实现的一个思路是, 将链表从中心拆成两半,后一半全部反向连接,然后前一半和后一半一个

Java for LeetCode 143 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}. 解题思路一: 每次将Ln换到前面,得到L0→Ln→L1→L2→L3→,然后对L1使用相同操作,

【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}. /** * Definition for singly-linked list. * clas

【leetcode】Reorder List (middle)

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}. 思路: 先把链表分成两节,后半部分翻转,然后前后交叉连接. 大神的代码比我的简洁,注意分两节时用快