Happy Learning Linked List 1 (Summary of Linked List Questions)

content

1. Binary linked list to integer

2. Delete the node in the linked list

3. Remove the linked list element

 4. Merge ordered list

5. The intermediate node of the linked list

6. Delete duplicate elements in linked list

write at the end


1. Binary linked list to integer

Original title link: 1290. Binary linked list to integer - LeetCode (leetcode-cn.com)

Idea: Convert node by node. If it is 1, add it. If it is 0, judge the next one. The addition of all data is a decimal number.

code show as below:

int getDecimalValue(struct ListNode* head){
    struct ListNode *p=head;
    int sum=0;
    while(p){
        sum+=p->val;
        p=p->next;
        if(p)
            sum<<=1;//左移,用来将一个数的各二进制位全部左移1位,右补0
    }
    return sum;
}

2. Delete the node in the linked list

Original title link: 237. Delete a node in a linked list - LeetCode (leetcode-cn.com)

Idea: Copy the data of the next node to the node that needs to be deleted.

code show as below:

void deleteNode(struct ListNode* node) {
    node->val=node->next->val;
    node->next=node->next->next;
}

3. Remove the linked list element

Original title link: 203. Remove linked list elements - LeetCode (leetcode-cn.com)

 Idea: Point the pointer field of the element node to be removed to the next node. If the node is at the end of the linked list, directly point the pointer field to NULL.

code show as below:

Hoichi:

struct ListNode* removeElements(struct ListNode* head, int val){
    struct ListNode *prev=NULL,*cur=head;//prev为cur的前一个节点
    while(cur)
    {
        if(cur->val==val)
        {
            if(cur==head)//所需移除元素在链表头部时
            {
                head=cur->next;
                cur=head;
            }
            else
            {
                prev->next = cur->next;
                free(cur);
                cur=prev->next;
            }
        }
        else
        {
            prev = cur;
            cur=cur->next;
        }
    }
    return head;
}

Houji:

struct ListNode* removeElements(struct ListNode* head, int val){
   while(1)
   {
       if(head==NULL)return NULL;//判断链表是否为空
       if(head->val==val)
       {
           head=head->next;
           continue;
       }//判断头节点
       struct ListNode *cur=head;
       while(cur){
           if(cur->val==val)
           {
               break;
           }
           cur=cur->next;
       }//遍历链表
       if(cur==NULL)return head;
       cur=head;
       while(cur->next->val!=val)
       {
           cur=cur->next;
       }//寻找删除元素
       struct ListNode *q;
       q=cur->next;
       cur->next=q->next;
       free(q);
   }
}

 4. Merge ordered list

Link to the original title: 21. Merge two ordered lists - LeetCode (leetcode-cn.com)

Ideas:

 code show as below:

struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2){
    if(l1==NULL){
        return l2;
    }
    if(l2==NULL){
        return l1;
    }//判断是否有空链表,若有则无需合并
    struct ListNode *head=NULL,*tail=NULL;
    while(l1&&l2)
    {
        if(l1->val < l2->val){
            if(head==NULL){
                head=tail=l1;
            }
            else{
                tail->next=l1;
                tail=l1;
            }
            l1=l1->next;
        }
        else{
            if(head==NULL){
                head=tail=l2;
            }
            else{
                tail->next=l2;
                tail=l2;
            }
            l2=l2->next;
        }
    }
//若两个链表其中一个已经到了链表尾部
    if(l1){
        tail->next=l1;
    }
    else{
        tail->next=l2;
    }
    return head;
}

5. The intermediate node of the linked list

Original title link: 876. The middle node of the linked list - LeetCode (leetcode-cn.com)

 Idea: Define two pointers, one fast and one slow, slow walks one node at a time, fast walks two nodes at a time, and slow just goes to the middle of the linked list when fast is finished.

code show as below:

struct ListNode* middleNode(struct ListNode* head){
    struct ListNode *slow=head,*fast=head;
    if(head==NULL){
        return NULL;
    }
    while(fast&&fast->next){
        slow=slow->next;
        fast=fast->next->next;
    }
    return slow;
}

6. Delete duplicate elements in linked list

Link to the original title: 83. Delete Duplicate Elements in a Sorted List - LeetCode (leetcode-cn.com)

 Idea: Define a pair of fast and slow pointers. If there are the same elements, free the latter node.

code show as below:

struct ListNode* deleteDuplicates(struct ListNode* head){
    struct ListNode* cur=head,*prev=NULL;
    if(head==NULL)return NULL;//判读链表是否为空
    if(head!=NULL)
    {
        prev=cur;
        cur=prev->next;
    }//判断前两个节点
//遍历链表
    while(cur){
        if(prev->val==cur->val)
        {
            prev->next=cur->next;
            printf("prev:%p\n",prev);
            free(cur);
            cur=prev->next;
            printf("prev:%p\n",prev);
        }
        else{
            prev=cur;
            cur=cur->next;
        }
    }
    return head;
}

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 everyone can point it out. If you have better code, I hope you can leave a message to the blogger. The blogger hopes to make progress with you on CSDN. , thanks everyone for watching!

Guess you like

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