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