Reorder List leetcode java
2017-08-17 20:25
645 查看
题目:
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
题解:
题目要重新按照 L0→Ln→L1→Ln-1→L2→Ln-2→…来排列,看例子1->2->3->4会变成1->4->2->3,拆开来看,是{1,2}和{4,3}的组合,而{4,3}是{3,4}的逆序。这样问题的解法就出来了。
第一步,将链表分为两部分。
第二步,将第二部分链表逆序。
第三步,将链表重新组合。
代码如下:
public void reorderList(ListNode head) {
if(head==null||head.next==null)
return;
ListNode slow=head, fast=head;
ListNode firsthalf = head;
while(fast.next!=null&&fast.next.next!=null){
slow = slow.next;
fast = fast.next.next;
}
ListNode secondhalf = slow.next;
slow.next = null;
secondhalf = reverseOrder(secondhalf);
while (secondhalf != null) {
ListNode temp1 = firsthalf.next;
ListNode temp2 = secondhalf.next;
firsthalf.next = secondhalf;
secondhalf.next = temp1;
firsthalf = temp1;
secondhalf = temp2;
}
}
public static ListNode reverseOrder(ListNode head) {
if (head == null || head.next == null)
return head;
ListNode pre = head;
ListNode curr = head.next;
while (curr != null) {
ListNode temp = curr.next;
curr.next = pre;
pre = curr;
curr = temp;
}
// set head node's next
head.next = null;
return pre;
}
Reference:http://www.cnblogs.com/springfor/p/3869333.html
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}.
题解:
题目要重新按照 L0→Ln→L1→Ln-1→L2→Ln-2→…来排列,看例子1->2->3->4会变成1->4->2->3,拆开来看,是{1,2}和{4,3}的组合,而{4,3}是{3,4}的逆序。这样问题的解法就出来了。
第一步,将链表分为两部分。
第二步,将第二部分链表逆序。
第三步,将链表重新组合。
代码如下:
public void reorderList(ListNode head) {
if(head==null||head.next==null)
return;
ListNode slow=head, fast=head;
ListNode firsthalf = head;
while(fast.next!=null&&fast.next.next!=null){
slow = slow.next;
fast = fast.next.next;
}
ListNode secondhalf = slow.next;
slow.next = null;
secondhalf = reverseOrder(secondhalf);
while (secondhalf != null) {
ListNode temp1 = firsthalf.next;
ListNode temp2 = secondhalf.next;
firsthalf.next = secondhalf;
secondhalf.next = temp1;
firsthalf = temp1;
secondhalf = temp2;
}
}
public static ListNode reverseOrder(ListNode head) {
if (head == null || head.next == null)
return head;
ListNode pre = head;
ListNode curr = head.next;
while (curr != null) {
ListNode temp = curr.next;
curr.next = pre;
pre = curr;
curr = temp;
}
// set head node's next
head.next = null;
return pre;
}
Reference:http://www.cnblogs.com/springfor/p/3869333.html
相关文章推荐
- LeetCode143之ReorderList的Java题解
- Reorder List leetcode java
- Java for LeetCode 143 Reorder List
- [Leetcode][JAVA] Reorder List
- LeetCode:Reorder List C++与Java实现
- leetcode reorder-list(java实现)
- LeetCode 9 Reorder List
- LeetCode 之 Reorder List
- LeetCode Reorder List
- 开始刷leetcode day 16:Reorder List
- LeetCode - Reorder List
- LeetCode :: Reorder List
- LeetCode: Reorder List
- LeetCode reorder-list
- LeetCode - Reorder List
- LeetCode:Reorder List
- LeetCode之Reorder List
- leetcode-Reorder List
- 【LEETCODE】143- Reorder List [Python]
- [Leetcode]Reorder List