The algorithm summarizes the single linked list in the way of the right and left half regions

Source: Internet
Author: User

Take the right side of the list and insert it into the linked list on the left.

Ideas:

If the list length is N, the method that gives the time complexity O (n) extra space complexity to O (1) is given directly

1 if the list is empty, or the length is 1 does not adjust

2 Linked list length is greater than 1 o'clock, traverse one to find the last node in the left half is recorded as mid

Package Tt;public class Test115 {public    class node{public    int value;    Public Node next;        public Node (int data) {    this.value=data;    }        }   public void Relocate (Node head) {     if (Head==null | | head.next==null) {      return;      }   Node mid = head;   Node right = Head.next;   while (Right.next!=null && right.next.next!=null) {   mid=mid.next;   Right=right.next.next;   }   Right=mid.next;   Mid.next=null;   MERGELR (head, right);   }      public void MERGELR (node-left, node-right) {   node-next = null;   while (left.next!=null) {   next=right.next;   Right.next=left.next;   Left.next=right;   Left=right.next;   Right=next;   }   left.next=right;   }       }

  

The algorithm summarizes the single linked list in the way of the right and left half regions

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.