[LeetCode] Reorder List, leetcodereorder

Source: Internet
Author: User

[LeetCode] Reorder List, leetcodereorder

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

/** * Definition for singly-linked list. * class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { *         val = x; *         next = null; *     } * } */

Question]

Given a linked list, insert the last node to 1st nodes, insert the last and second nodes to 2nd nodes, insert the last and third nodes to 3rd nodes, and so on ......

[Idea]

From the meaning of the question, the following (n-1)/two nodes need to be inserted to the Front (n-1)/two nodes respectively.

The linked list is divided into two sections. The first n-(n-1)/two nodes are inserted into the linked list, and the last n-(n-1)/two nodes are inserted into the linked list.

Before inserting, you need to sort the inserted linked list in reverse order, that is, the nth node-> the n-1 node->...

[Java code]

Public class Solution {public void reorderList (ListNode head) {ListNode node = head; int cnt = 0; while (node! = Null) {cnt ++; node = node. next;} if (cnt <3) return; // knots below 3 do not need to move int k = (cnt-1)/2; // The last k nodes to be moved: int I = 1; node = head; while (I ++ <cnt-k) {node = node. next;} ListNode begin = node. next; // use begin to indicate the Start node of the last k nodes to be moved. next = null; // set the end of the part that does not need to be moved to null. // set the k nodes to be moved to the reverse order of ListNode pre = begin; ListNode cur = begin. next; begin. next = null; while (cur! = Null) {ListNode next = cur. next; cur. next = pre; begin = cur; pre = cur; cur = next;} ListNode node1 = head; ListNode node2 = begin; while (node1! = Null & node2! = Null) {pre = node1; cur = node2; node1 = node1.next; // these two rows must be placed before the following two rows, because pre and node1 point to the same node, the following operation will change the next of node1. The same reason is node2 = node2.next and cur. next = pre. next; // these two lines of code insert cur into pre-post-pre. next = cur ;}}}

[Feelings]

The code is too disgusting to write, and it will be confusing to write it. Later, I will not know which node next refers.



[Upgrade]

First, use the speed pointer to find the midpoint of the linked list, flip the second half of the linked list, and then combine it with the first half.

Note that when the linked list is divided into two halves, the End Node of the first half of the segment should be set to NULL, and the end node should also be set to NULL when the linked list is flipped.

Public class Solution {public void reorderList (ListNode head) {if (head = null | head. next = null) return; // divides the entire Linked List into two sub-linked lists with an equal length. If the original linked list has an odd length, the length of the first sub-linked list is 1 ListNode slow = head, fast = head; while (fast. next! = Null) {fast = fast. next; if (fast. next! = Null) fast = fast. next; else break; slow = slow. next;} ListNode head1 = head, head2 = slow. next; slow. next = null; // flip the second sublinked list ListNode cur = head2, post = cur. next; cur. next = null; while (post! = Null) {ListNode tmp = post. next; post. next = cur; cur = post; post = tmp;} head2 = cur; // merge two sublinked lists into ListNode node1 = head1, node2 = head2; while (node2! = Null) {ListNode tmp1 = node1.next; ListNode tmp2 = node2.next; node1.next = node2; node2.next = tmp1; node1 = tmp1; node2 = tmp2 ;}}}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.