Code Caprice [Linked List] ----> Reverse Linked List, Pairwise Exchange of Nodes in Linked List

206. Reverse Linked List

题目LeetCode206. Reverse Linked List
insert image description here

train of thought

指针域Flipping the linked list actually only needs to point to each nodeprevious nodeThat is, the pointer field of the original first node points to the NULL pointer.
insert image description here
The original head node is 1, and the flipped head node is 5. The flipping of the linked list only changes the pointing of the pointer field of each node in the linked list.
The specific flipping process ( correction: In the animation, pre should be moved first, then cur )
insert image description here

  • First, two pointers cur and pre are defined, and each time another cur pointer points to a node, the pointer field points to the node pointed to by the pre pointer. After flipping a group of elements, move the pre and cur pointers backward by one node respectively
  • Until the cur pointer becomes a NULL pointer, the flip of the linked list is completed at this time, pre becomes the head node pointer, return pre

Notice:

  • The pre pointer should be NULL at the beginning, in order to make the pointer field of the first node point to the NULL pointer
  • When the cur pointer points to the next node, the address of the next node should be saved first, because the node pointed to by cur is flipped

The next step is the code implementation. The following introduces the two code implementations of double pointer and recursion. You must understand recursion on the basis of understanding double pointers, because the recursive code is relatively short and difficult to understand, but the core idea and double pointer version are exactly the same


Double pointer implementation

//双指针
struct ListNode* reverseList(struct ListNode* head){
    
    
    typedef struct ListNode ListNode;
   ListNode* cur = head;
   //pre指针为NULL是为了让第一个节点翻转为尾节点
   ListNode* pre = NULL;
   //cur为NULL时结束循环,pre为翻转后的头节点
   while (cur)
   {
    
    
       ListNode* tmp = cur->next;//将当前节点的下一个节点存起来
       cur->next = pre;//反转节点
       pre = cur;//pre指针指向下一个节点
       cur = tmp;//cur指针之下下一个节点
   }
   //返回新的头节点
   return pre;
}

insert image description here


recursive writing

//递归写法
 typedef struct ListNode ListNode;
ListNode* reverse(ListNode* pre, ListNode* cur)
{
    
    
    /*
    while (cur)
    return pre
    这个步骤等价于下面*/
    if (cur == NULL)    return pre;
    //反转
    ListNode* tmp = cur->next;
    cur->next = pre;
    /*
    pre = cur;
    cur = tmp;
    这个步骤等价于下面*/
    return reverse(cur, tmp);
}
 struct ListNode* reverseList(struct ListNode* head){
    
    
    
     return reverse(NULL, head);
 }

insert image description here

The reverse function in the recursive writing method is to point the pointer field of the node pointed to by cur to the node pointed to by pre, so after flipping one element each time, it is necessary to flip the next element until the entire linked list is flipped. Flip the next element and directly pass cur as a formal parameter to pre, and pass tmp (the next node of cur before flipping) to cur


24. Exchange the nodes in the linked list two by two

题目LeetCode24. Exchange nodes in a linked list in pairs
insert image description here

train of thought

PS: I personally think that if a question will change the address of the head node, then this question can be completed by using the virtual head node, because through the virtual head node, we only need to return the pointer field of the virtual head node in the end, and we don’t need to care about modifying the linked list The address of the head node after
this question will exchange two nodes of the linked list, so the original head node will definitely become the intermediate node of the modified linked list, and the original intermediate node will eventually become the head node of the new linked list, so for this question The address of the head node has changed, so the virtual head node can be used

Now let's look at the specific process

  1. After exchanging the first two nodes
    insert image description here
    , the linked list becomes like this
    insert image description here
  2. Continue to exchange the following elements, let cur point to the last two nodes of the previous cur, repeat process 1
  3. Stop the loop until cur->next (even number of nodes) or cur->next->next (odd number of nodes) is empty

Notice:

  • Before executing step 2, you need to save the address of the next node pointed to by cur so that the second node behind cur can point to the first node behind cur
  • To execute step 3, it is necessary to store the address of the third node behind cur so that the first node behind cur can point to the third node behind cur

Code

struct ListNode* swapPairs(struct ListNode* head){
    
    
    typedef struct ListNode ListNode;
    //虚拟头节点的创建
    ListNode* dummyHead = (ListNode*)malloc(sizeof(ListNode));
    dummyHead->next = head;
    ListNode* cur = dummyHead;
    //考虑奇数和偶数个节点
    while (cur->next != NULL && cur->next->next != NULL)
    {
    
    
        //将cur后面第1、3个节点的地址存起来
        ListNode* tmp1 = cur->next;
        ListNode* tmp2 = cur->next->next->next;
        //步骤一
        cur->next = cur->next->next;
        //步骤二
        cur->next->next = tmp1;
        //步骤三
        tmp1->next = tmp2;
        //cur移动两位、准备下一位的交换
        cur = cur->next->next;
    }
    return dummyHead->next;
}

insert image description here

Guess you like

Origin blog.csdn.net/m0_74278159/article/details/130045628