[Leetcode] reorder list

Source: Internet
Author: User

Given a singly linked listL:L0 →L1 →... →LN-1 →LN,
Reorder it:L0 →LNL1 →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}.

 

Question: It seems that the sequence of the chain table obtained after the last transformation is very strange. The starting point is very simple. It is to break the chain table from the center, reverse the half, and merge with the first half. However, this sort is not to take a small value from the two linked lists each time, but to staggered the two linked lists.

For example, 1-> 2-> 3-> 4, break the link in the middle to get two linked lists 1-> 2 and 3-> 4, and the next linked list is reversed to 4-> 3, then it is staggered with the first linked list to get 1-> 3-> 2-> 4;

For example, 1-> 2-> 3-> 4-> 5, break two linked lists from the center, 1-> 2-> 3 and 4-> 5, the next linked list is reversed to 5-> 4, and the first linked list is staggered and merged to 1-> 4-> 2-> 5-> 3;

The Code is as follows:

 1 /** 2  * Definition for singly-linked list. 3  * class ListNode { 4  *     int val; 5  *     ListNode next; 6  *     ListNode(int x) { 7  *         val = x; 8  *         next = null; 9  *     }10  * }11  */12 public class Solution {13      private ListNode reverse(ListNode head){14          ListNode newHead = null;15          while(head != null){16              ListNode temp = head.next;17              head.next = newHead;18              newHead = head;19              head = temp;20          }21          return newHead;22      }23      24      private void newMerge(ListNode head1,ListNode head2){25          ListNode newHead = new ListNode(0);26          27          while(head1 != null && head2 != null){28              newHead.next = head1;29              head1 = head1.next;30              newHead = newHead.next;31              newHead.next = head2;32              head2 = head2.next;33              newHead = newHead.next;34          }35          if(head1 != null)36              newHead.next = head1;37          if(head2 != null)38              newHead.next = head2;39      }40      private ListNode findMiddle(ListNode head){41           ListNode slow = head;42           ListNode fast = head.next;43           while(fast != null && fast.next != null)44           {45               slow = slow.next;46               fast = fast.next.next;47           }48           return slow;49       }50     public void reorderList(ListNode head) {51         if(head == null || head.next == null)52             return;53         54         ListNode mid = findMiddle(head);55         ListNode tail = reverse(mid.next);56         mid.next = null;57         58         newMerge(head, tail);59     }60 }

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.