[Leedcode] Necessary interview questions for linked lists in data structures (Phase 3)

[Leedcode] Necessary interview questions for linked lists in data structures (Phase 3)



1. The first question

1. Topic

The CM11 linked list is divided as follows (example):

现有一链表的头指针ListNode*pHead,给一定值x,
编写一段代码将所有小于x的结点排在其余结点之前,且不能改变原来的数据顺序,返回重新排列后的链表的头指针。 

insert image description here


2. Ideas

1. Use the head nodes lesshead and greathead with two sentinel positions , connect the ones less than 4 to the back of lesshead, and the ones greater than 4 to the back of greathead;
2. Then connect the last one less than 4 to the first one greater than 4: Specifically as shown below


![Insert picture description here](https://img-blog.csdnimg.cn/ddf62b3dab8a4de7aea2d12a4549626d.png


Note: as shown below!
insert image description here


3. Source code

The code is as follows (example):

struct ListNode {
    
    
    int val;
    struct ListNode *next;
};
class Partition {
    
    
public:
    ListNode* partition(ListNode* pHead, int x) {
    
    
        struct ListNode* lessHead, *lessTail, *greaterHead, *greaterTail;
        lessHead = lessTail = (struct ListNode*)malloc(sizeof(struct ListNode));
        greaterHead = greaterTail = (struct ListNode*)malloc(sizeof(struct ListNode));
        
        lessTail->next = greaterTail->next = NULL;
        
        struct ListNode* cur = pHead;
        
        while (cur) {
    
    
            if (cur->val < x) {
    
    
                lessTail->next = cur;
                lessTail = lessTail->next;
            }
            else {
    
    
                greaterTail->next = cur;
                greaterTail = greaterTail->next;
            }
            cur = cur->next;
        }
        
        lessTail->next = greaterHead->next;
        greaterTail->next = NULL;
        
        struct ListNode* list = lessHead->next;
        free(lessHead);
        free(greaterHead);
        
        return list;
    }
};

2. The second question

1. Topic

The palindrome structure of OR36 linked list is as follows (example):

对于一个链表,请设计一个时间复杂度为O(n),额外空间复杂度为O(1)的算法,判断其是否为回文结构。

给定一个链表的头指针A,请返回一个bool值,代表其是否为回文结构。保证链表长度小于等于900

insert image description here


2. Ideas

(1) The first case: an even number of linked lists


insert image description here


(2) The second case: an odd number of linked lists

insert image description here


3. Source code

(1) Implementation of the intermediate node of the linked list

  1. The implementation of the intermediate node of the linked list is as follows (example):
struct ListNode
{
    
    
     int val;
    struct ListNode *next;
}
struct ListNode* middleNode(struct ListNode* head)
{
    
    
    struct ListNode* slow,*quick;
    slow=quick=head;
    while(quick && quick->next)
    {
    
    
        slow= slow->next;
        quick= quick->next->next;
    }
    return slow;
}

(2) Implementation of reverse linked list

  1. The implementation of reverse linked list is as follows (example):
struct ListNode 
{
    
    
    int val;
     struct ListNode *next;
};
struct ListNode* reverseList(struct ListNode* head)
{
    
    
    //第二种方法
    struct ListNode* newhead=NULL;
    struct ListNode* cur=head;
    while(cur)
    {
    
    
        struct ListNode* next=cur->next;
        cur->next=newhead;
        newhead=cur;
        cur=next;
    }
    return newhead;
}

(3) Implementation of linked list comparison function

The code is as follows (example):

class PalindromeList {
    
    
public:
    bool chkPalindrome(ListNode* A) {
    
    
        struct ListNode* mid =middleNode(A);
        struct ListNode* rHead =reverseList(mid);

        struct ListNode* curA=A;
        struct ListNode* curR = rHead;
        while( curA && curR)
        {
    
    
            if(curA -> val != curR ->val)
            {
    
    
                return false;
            }
            else {
    
    
                curA=curA->next;
                curR =curR-> next;
            }
        }
        return true;
    }
};

(4) Overall source code

The code is as follows (example):

struct ListNode 
{
    
    
    int val;
    struct ListNode *next;
    ListNode(int x) : val(x), next(NULL) {
    
    }
};
struct ListNode* middleNode(struct ListNode* head)
{
    
    
    struct ListNode* slow,*quick;
    slow=quick=head;
    while(quick && quick->next)
    {
    
    
        slow= slow->next;
        quick= quick->next->next;
    }
    return slow;
}
struct ListNode* reverseList(struct ListNode* head)
{
    
    
    //第二种方法
    struct ListNode* newhead=NULL;
    struct ListNode* cur=head;
    while(cur)
    {
    
    
        struct ListNode* next=cur->next;
        cur->next=newhead;
        newhead=cur;
        cur=next;
    }
    return newhead;

}
class PalindromeList {
    
    
public:
    bool chkPalindrome(ListNode* A) {
    
    
        struct ListNode* mid =middleNode(A);
        struct ListNode* rHead =reverseList(mid);

        struct ListNode* curA=A;
        struct ListNode* curR = rHead;
        while( curA && curR)
        {
    
    
            if(curA -> val != curR ->val)
            {
    
    
                return false;
            }
            else {
    
    
                curA=curA->next;
                curR =curR-> next;
            }
        }
        return true;
    }
};

Summarize

The above is what I want to talk about today. This article introduces the necessary interview questions for linked lists in data structures (the third issue).
If my blog is helpful to you, remember to support it three times. Thank you for your support!

insert image description here

Guess you like

Origin blog.csdn.net/2201_75587702/article/details/129188556