Java for LeetCode 143 Reorder List

Given a singly linked list L: L0L1→…→Ln-1Ln,
reorder it to: L0LnL1Ln-1L2Ln-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换到前面,得到L0LnL1L2L3→,然后对L1使用相同操作,JAVA实现如下:

	public void reorderList(ListNode head) {
		ListNode headCopy = head;
		while (headCopy != null && headCopy.next != null
				&& headCopy.next.next != null) {
			ListNode temp = headCopy;
			while (temp.next.next != null)
				temp = temp.next;
			temp.next.next = headCopy.next;
			headCopy.next = temp.next;
			temp.next = null;
			temp = headCopy.next.next;
			headCopy=headCopy.next.next;
		}
	}

结果TLE

解题思路二:

空间换时间,将所有的node存到一个list中,然后每次操作list头尾两个node即可,JAVA实现如下:

    public void reorderList(ListNode head) {
		LinkedList<ListNode> list = new LinkedList<ListNode>();
		ListNode headCopy = head,end = head;
		while (headCopy != null) {
			list.add(headCopy);
			headCopy = headCopy.next;
		}
		while(list.size()>2){
			headCopy=list.poll();
			end=list.get(list.size()-1);
			list.remove(list.size()-1);
			headCopy.next=end;
			end.next=list.peek();
			list.get(list.size()-1).next=null;
		}
    }
时间: 2024-08-11 09:55:45

Java for LeetCode 143 Reorder List的相关文章

leetcode 143. 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.使用list记录链表. /** * Definition for si

[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 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: G

143. Reorder List - LeetCode

Question 143. Reorder List Solution 题目大意:给一个链表,将这个列表分成前后两部分,后半部分反转,再将这两分链表的节点交替连接成一个新的链表 思路 :先将链表分成前后两部分,将后部分链表反转,再将两部分链表连接成一个新链表 Java实现: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x)

Java for LeetCode 216 Combination Sum III

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. Example 1

Java for LeetCode 128 Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run i

Java for LeetCode 098 Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

Java for LeetCode 057 Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], insert and merge

Java for LeetCode 059 Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]] 解题思路: 参考Java for LeetCode 054 Spiral Matrix,修改下

Java for LeetCode 107 Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order trave