Summary of linked list OJ questions 2

content

1. Reverse the linked list

2. Palindromic linked list

3. Circular linked list

4. Replication of complex linked lists

write at the end

1. Reverse the linked list

Link to the original title: The Sword Points to Offer II 024. Reverse Linked List - LeetCode (leetcode-cn.com)

Idea: Create a new linked list and insert the element headers from the original linked list into the new linked list.

code show as below:

struct ListNode* reverseList(struct ListNode* head){
    struct ListNode *p=NULL;
    struct ListNode *cur=head;
    while(cur){
        struct ListNode *next=cur->next;
        cur->next=p;
        p=cur;
        cur=next;
    }
    return p;
}

2. Palindromic linked list

Link to the original title: Sword refers to Offer II 027. Palindrome Linked List - LeetCode (leetcode-cn.com)

 Idea: First define a fast pointer fast, a slow pointer slow, each time slow goes one node, fast goes two nodes, then reverse the node behind the slow, and compare the reversed linked list with the previous unreversed linked list one by one , to judge whether they are the same, if they are different, return false, if they are all the same, return true.

code show as below:

//逆置链表
struct ListNode* reverse(struct ListNode* head){
    struct ListNode* prev=NULL,*cur=head;
    while(cur){
        struct ListNode* next=cur->next;
        cur->next=prev;
        prev=cur;
        cur=next;
    }
    return prev;
}

bool isPalindrome(struct ListNode* head){
    struct ListNode* slow=head,*fast=head;
    while(fast&&fast->next){
        slow=slow->next;
        fast=fast->next->next;
    }
    //逆置后半部分链表
    struct ListNode* hui=reverse(slow);
    fast=head;
    while(hui&&fast){
        if(fast->val!=hui->val)return false;
        hui=hui->next;
        fast=fast->next;
    }
    return true;
}

3. Circular linked list

Original title link: 141. Circular linked list - LeetCode (leetcode-cn.com)

 Idea: If the linked list has a cycle, if no other conditions are imposed, it will continue to point in the process of traversal, so the node of element 2 will meet twice, so if a node is encountered twice, it means that the linked list has a cycle.

code show as below:

bool hasCycle(struct ListNode *head) {
    struct ListNode *fast=head,*slow=head;
    while(fast && fast->next)
    {
        slow=slow->next;
        fast=fast->next->next;
        if(fast==slow)
        {
            return true;
        }
    }
    return false;
}

4. Replication of complex linked lists

Link to the original title: Sword refers to Offer 35. Copy of complex linked list - LeetCode (leetcode-cn.com)

 Ideas:

①Add a new linked list after each node of the original linked list

 

② connect the random of each node

③ Cut the copied node

code show as below:

class Solution {
public:
    Node* copyRandomList(Node* head) {
        struct Node *cur=head;
        //创建复制的节点
        while(cur){
            struct Node *copy=(struct Node*)malloc(sizeof(struct Node));
            copy->val=cur->val;
            //插入copy节点
            copy->next=cur->next;
            cur->next=copy;

            cur=copy->next;
        }
        //复制random
        cur=head;
        while(cur){
            struct Node *copy=cur->next;
           if(cur->random==NULL)
           {
               copy->random=NULL;
           }
           else{
               copy->random=cur->random->next;
           }
           cur=copy->next;
        }
        //剪下复制的节点
        struct Node *CopyHead=NULL,*CopyTail=NULL;
        cur=head;
        while(cur){
            struct Node *copy=cur->next;
            struct Node *next=copy->next;
            if(CopyTail==NULL)
            {
                CopyHead=CopyTail=copy;
            }
            else
            {
                CopyTail->next=copy;
                CopyTail=copy;
            }
            cur->next=next;
            cur=next;
        }
        return CopyHead;
    }
};

write at the end

The above is the entire content of this article. The author's knowledge level is limited. If there are any mistakes or improvements, I hope you will point out. If you have better code or other high-quality topics, I hope you can leave a message to the blogger. CSDN progresses with you, thank you for watching!

Guess you like

Origin blog.csdn.net/qq_61139806/article/details/124461022