C language list inversion II (two specified nodes inversion)

Claim:

Reverse list from the position m to n. Please use the trip to the scan is complete reversal.

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

Example:

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

Iterative reverse link

algorithm

Before looking at the specific algorithm, it is necessary to clarify the principle of reverse link and which require pointer. For example, three different nodes have a list consisting of A → B → C, the reverse link need to be in the node A ← B ← C.

Suppose we have two pointers, pointing to a node A, a node point B. They were recorded as prev and cur. The two pointers can be realized simply inverting the link between A and B:

cur.next = prev
do so only problem is, there is no way to continue, in other words, after doing so you no longer have access to the node C. Therefore, we need to introduce a third pointer for help reverse the process. Therefore, we do not use the above inversion method, but:

= cur.next THIRD
cur.next = PREV
PREV = CUR
CUR = THIRD
above procedure iteratively to complete the requirements of the problem. Let's look at the steps of the algorithm.

1. As mentioned above, we need two pointers prev and cur.
2.prev pointer is initialized to None, cur pointer is initialized to the list head.
3. step by step to move forward cur pointer, prev pointer follow.
4. Thus advancing two pointers, until the pointer reaches cur mm of nodes starting from the head of the list. This is the starting position we reverse the list.
5. Notice that we want to introduce two additional pointers, called tail and con. the tail pointer points to the first nodes mm starting from the head of the list, this junction is reversed and the tail of the list, so called tail. con pointer to the first node before a mm-node, this node is the new head of the list. The following diagram can help you better understand these two pointers.

 

6.tail and con pointer at the beginning of the algorithm is initialized, the last is called algorithms for complete list reversal.
7. As previously explained, after the arrival of nodes mm, before the use of the two pointers, to iteratively reverse link. Continue to iterate until the completion of the link to the first nn-node. At this time, prev pointer will point to the first nodes nn.
8. we use con prev pointer is connected to a pointer, because the prev pointer nodes (first nodes nn) will replace the current pointing position of nodes mm. Node (the n + 1n + 1 nodes) after Similarly, we use the tail pointer connected prev pointer.
In order to facilitate the usage of each pointer to sort out, we look at an example of running the algorithm. Given a list 7 → 9 → 2 → 10 → 1 → 8 → 6, we need to reverse a linked list of nodes from the third to the sixth node

 

 

 

From the map you can see the first steps of iteration method. The first step shows the initialization of two pointers, the third step shows the initial position in the list of the reversing process.

 

 

The figure shows in detail the process and how to reverse link moves forward and backward reverse link two nodes. As shown below, this step will be performed multiple times.

 

如上图所示, 两个指针都已经到达最终位置。我们完成了子链表的反转工作。然而,还有一些链接需要调整。下图展示了利用 tail 和 con 指针完成链接调整的过程。

 

 


以上链接:https://leetcode-cn.com/problems/reverse-linked-list-ii/solution/fan-zhuan-lian-biao-ii-by-leetcode/

 Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

typedef struct ListNode * PNODE ;
typedef struct ListNode NODE ;
struct ListNode* reverseBetween(struct ListNode* phead, int m, int n){
    PNODE prev=NULL ;
    PNODE curr=phead;
    PNODE temp=NULL ;
    PNODE con =NULL ;
    PNODE tail=NULL ;
   if(phead==NULL)
   {
        return NULL ;
   }
   while(m>1)
   {
    prev=curr;
    curr=curr->next;
    m-- ;
    n-- ;
   }
   con=prev ;
   tail=curr ;
   while(n>0)
   {
    temp=curr->next;
    curr->next=prev;
    prev=curr;
    curr=temp;
    n--;
   }
   //调整算法连接
   if(con!=NULL)
   {
    con->next=prev ;
   }
   else
   {
    phead=prev ;
   }
   tail->next=curr ;
   return phead ;
}

Guess you like

Origin www.cnblogs.com/cocobear9/p/12370081.html