Sword refers to offer question 25 (C#)

Sword refers to offer question 25 (C#)

Title Description
Input a complex linked list (each node has a node value and two pointers, one points to the next node, and the other special pointer random points to a random node), please make a deep copy of this linked list, and return the copied head node. (Note, please do not return the node reference in the parameter in the output result, otherwise the judging program will return empty directly)

The main point of this question is to be able to sort out the logic clearly. The method I used is a popular solution on the Internet, copying the entire linked list and then splitting it back to the complex linked list after deep copying. Borrowing the three pictures made by netizens,

it can be seen intuitively that the first step of inserting and copying the original linked list is to copy and insert the next in the linked list.
insert image description here
The next step is to copy and intersperse the random. The second step needs to rely on the linked list copied in the first step to continue to copy the relationship. (I will explain in detail in the code)
insert image description here
Finally, divide the copied large linked list and return the latter to the output. Well, the general logic is like this. Let's take a look at the code implementation: show some below 内联代码片.

    public RandomListNode Clone(RandomListNode pHead)
    {
    
    
        // write code here
        if(pHead==null)
           return null;       
        RandomListNode head = pHead;
        RandomListNode head2 = pHead;//这里将头节点复制两遍在后面复制两块逻辑用上        
        while(pHead!=null)
        {
    
    
            RandomListNode res = new RandomListNode(pHead.label);
            res.next = pHead.next;
            pHead.next = res;
            pHead = res.next;//第一部分的模块链表建议大家参照第一张图片配合食用效果更佳,就是简单的单链表插入操作。
        }
        while(head!=null)
        {
    
    
            if(head.random!=null)
                head.next.random = head.random.next;//原来的节点指向的random赋值到复制之后A’节点的random去
            head = head.next.next;//第二部分复制random模块,同样建议大家配合第二张图食用,
            //利用第一段代码的基础下把原来的节点指向的random赋值到复制之后A’节点的random去。
        }
        RandomListNode head3 = head2.next;//head3赋值为复制之后的链表头节点做输出头节点。
        RandomListNode result = head3;//从result来开始拼接起来整个深拷贝链表的每个节点。
        head = head2;//从重合的大链表头指针开始循环切分
        while(head!=null)
        {
    
    
            head.next = head.next.next;//原链表两个两个一切之后缝合。
            if(result.next!=null)
                result.next = result.next.next;//深拷贝链表两个两个一切之后缝合。
            head = head.next;
            result = result.next;//循环递增指向下一个两个分链表各自节点。
        }        
        return head3;
    }

This question mainly lies in the logic of the three pictures. After thinking and clarifying the logic of the three steps, it can be solved step by step. It does not involve too much syntax and the use of linked list methods. Test your logical arithmetic ability.

Guess you like

Origin blog.csdn.net/weixin_50746193/article/details/116657746