Advanced reverse list

Recent courses tight, Mid-Autumn Festival holiday two days of rest, come back to brush algorithm problem. Flip the list is a relatively common type, to thoroughly understand.

 

Power button 92 Title: Title Description: reversed from the position  m  to  n  of the linked list. Please use the trip to the scan is complete reversal.

Description:. 1  ≤  m  ≤  n-  ≤ chain length.

Example:

Input: 1-> 2-> 3-> 4- > 5-> NULL, m = 2, n = 4 
Output: l-> 4-> 3-> 2-> 5-> NULL

Ideas:

To reverse end-node pointer recording interval, a write interval m to n partial inversion function, the link will be reversed up

    public  static ListNode reverseBetween (ListNode head, int m, int n-) { 

        IF (== n-m) return head;
         // Create the first node 
        ListNode PHEAD = new new ListNode (0 ); 
        pHead.next = head; 
        ListNode tail = PHEAD ;
         // find the need to reverse a previous node that period 
        for ( int I =. 1; I <m; I ++ ) 
            tail = tail.next;
         // partially inverted list of new starting point 
        ListNode nextHead = tail.next;
        Pre ListNode =null ; 
        ListNode Next = null ;
         // reversing this period n to m, i.e., n-m + 1 inversion nodes 
        for ( int I = 0; I <= n - m; I ++ ) { 
            Next = nextHead.next; 
            nextHead.next = pre; 
            pre = nextHead; 
            nextHead = Next; 
        } 
        // pre partially inverted list is the head node 
        tail.next = pre;
         // find the last node in the list reverse 
        the while (pre.next =! null ) { 
            pre = Pre.next; 
        } 
        // linked 
        pre.next = Next;
         return pHead.next; 
    }

 

Stay button Question 25: k a a set of counter-list

Subject description:

Given a list, each  k  th flip a set of nodes, and returns the list after inversion.

k  is a positive integer that is less than or equal to the length of the list. If the total number of nodes is not  k  is an integer multiple of, the last remaining node original order.

Example:

Given this list: 1-> 2-> 3-> 4-> 5 

when K = 2, should be returned: 2-> 1-> 4-> 3-> 5 

When K = 3, the should be returned: 3 -> 2-> 1-> 4-> 5

Description:

  • You can only use the extra space algorithm constants.
  • You can not just simply change the internal node values, but need to be the actual node exchange.

Idea: reverse pointer records need to be part of your head and tail node, a partial reversal of the write function, every k reversed once, not to return to the K

public  static ListNode reverseKGroup (ListNode head, int k) {
        IF (head == null || head.next == null || k <=. 1 )
            return head; 
       ListNode the currentNode = head;
        // Get the node k and last elements 
        for ( int COUNT =. 1; COUNT <k; COUNT ++ ) { 
            the currentNode = currentNode.next;
             // not return the k 
            IF (the currentNode == null )
                 return head; 
        } 
        ListNode Next= CurrentNode.next;
         // local inverted list 
        Reverse (head, the currentNode);
         // recursive then inverted once every K 
        head.next = reverseKGroup (Next, K);
         return the currentNode; 
    } 

 public  static ListNode Reverse ( head ListNode, ListNode tail) {
         IF (head == null || head.next == null )
             return head; 
        ListNode pre = null ; 
        ListNode Next = null ;
         the while ! (pre = tail) { 
            Next= head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        return head;
    }

 

Stay button 143 title: rearrangement list

Subject description:

Given a single chain  L : L 0 → L . 1 → ... → L ** n- -1 → L n-, after rearranging becomes:  L 0 → L ** n-L . 1 → L ** n- -1 → L 2 → L ** n- -2 → ...

You can not just simply change the internal node values, but need to be the actual node exchange.

Example:

Given list 1-> 2-> 3-> 4, is rearranged 1-> 4-> 2-> 3 . 
Given list 1-> 2-> 3-> 4-> 5, 1- rearranged > 5-> 2-> 4-> 3.

Ideas: Find the list to the intermediate node, the list is divided into two sections, the second half of the inversion, merge in the two lists

 public  static  void ReorderList (ListNode head) {
         IF (head == null || head.next == null )
             return ; 
        ListNode P1 = head; 
        ListNode P2 = head;
         // Find the list of the intermediate node to 
        the while (p2.next! = null ! && p2.next.next = null ) { 
            P1 = p1.next; 
            P2 = p2.next.next; 
        } 
        // the list is divided into two sections 
        P2 = p1.next; 
        p1.next= Null ; 
        P1 = head;
         // the second half is inverted list 
        ListNode pre = null ; 
        ListNode Next = null ; 
        ListNode head2 The = P2;
         the while (! = Head2 The null ) { 
            Next = head2.next; 
            head2.next = pre; 
            pre = head2 The; 
            head2 The = Next; 
        } 
        P2 = pre;
         // merge two lists
        ListNode next1;
        ListNode next2;
        while (p2 != null) {
            next1 = p1.next;
            next2 = p2.next;
            p1.next = p2;
            p2.next = next1;
            p1 = next1;
            p2 = next2;
        }
    }

 

Guess you like

Origin www.cnblogs.com/zengcongcong/p/11521719.html