C language singly linked list OJ problem (easy)

1. Remove linked list elements

leetcode link

Title description:

Given a head node of a linked list  head and an integer  val , please delete all satisfied  Node.val == val nodes in the linked list and return  a new head node  .

Ideas:

Normal traversal, find the value of the value and the same node in the title to free, there are two situations:

The first is that the head node is the value value, directly pointing the head node to next;

The second case is that the second node starts with value, and there needs to be a previous node pointing to the next node of value.

source code:

struct ListNode* removeElements(struct ListNode* head, int val)
{
    //链表本身为空
    if(head==NULL)
        return NULL;
    struct ListNode* prev = NULL;
    while(1)//头节点开始就是值
    {
        if(head->val==val)
        {
            prev=head;
            head=head->next;
            free(prev);
            if(head==NULL)
            {
                return NULL;
            }
        }
        else
        {
            break;
        }
    }
    struct ListNode* cur = head;
    while(cur)//从第二个开始才是value,可以使用prev,因为第一个不是value,可以存储
    {
        //这一部分卡了好久
        if(cur->val==val)
        {
            prev->next=cur->next;    
            struct ListNode* del=cur;
            free(del);
            cur=prev->next;
        }
        else
        {
            prev=cur;
            cur=cur->next;
        }
    }
    return head;
}

Second, the intermediate node of the linked list

leetcode link

Title description:

Given the head node of the singly linked list  head , please find and return the middle node of the linked list.

If there are two intermediate nodes, return the second intermediate node.

Ideas:

Use the fast and slow pointer method .

Carry out a cycle, first let the fast pointer take two steps, then let the slow pointer take one step , until the fast pointer points to empty or the next of the fast pointer points to empty, then the slow pointer points to the middle node, and also satisfies the second Node.

code:

struct ListNode* middleNode(struct ListNode* head)
{
    //快慢指针,快指针走两步,慢指针走一步
    struct ListNode* slow = head;
    struct ListNode* fast = head;
    while(fast&&fast->next)
    {
        fast=fast->next->next;
        slow=slow->next;
    }
    return slow;
}

3. The penultimate k node in the linked list

Niuke.com link

Title description:

Input a linked list and output the kth last node in the linked list.

Ideas:

It is also the idea of ​​fast and slow pointers .

Let the fast pointer go k steps first, and then let the fast pointer and the slow pointer go together until the fast pointer is empty .

This question has some points to pay attention to details, such as k is greater than the number of linked list nodes, k==0, but these are small details, the main idea is still the speed pointer~

 code:

struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) {
    // write code here
    if(pListHead==NULL||k==0)//k=0和链表为空的情况
    {
        return NULL;
    }
    struct ListNode* fast=pListHead;
    struct ListNode* slow=pListHead;
    while(k--)//先让快指针走k步
    {
        fast=fast->next;
        if(fast==NULL)
        {
            break;
        }
    }
    if(k>0)//k大于链表结点的个数的情况
    {
        return NULL;
    }
    while(fast)
    {
        fast=fast->next;
        slow=slow->next;
    }
    return slow;
}

Fourth, reverse the linked list

leetcode link

Title description:

Given the head node of the singly linked list  head , please reverse the linked list and return the reversed linked list.

Ideas:

Traverse the linked list sequentially, starting from the first node for tail insertion . Note that the tail insertion here is not the pushback function in the hand-torn singly linked list, but the nodes should be removed one by one.

It is equivalent to learning another way of tail insertion, instead of directly changing the value of the head node, but pointing the original head node to the new head node.

code:

struct ListNode* reverseList(struct ListNode* head)
{
    struct ListNode* newhead=NULL;
    struct ListNode* next=NULL;
    //头插
    while(head)
    {
        next=head->next;
        head->next=newhead;
        newhead=head;
        head=next;
    }
    return newhead;
}

 5. Merge two ordered linked lists

leetcode link

Title description:

Merges two ascending lists into a new  ascending  list and returns. The new linked list is formed by splicing all the nodes of the given two linked lists.

Ideas:

Compare the order of the first nodes of the two linked lists, take the smaller tail and insert it into a new head node , if one ends early, insert the other directly into the new linked list

code:

struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2)
{
    if(list1==NULL)
    {
        return list2;
    }
    if(list2==NULL)
    {
        return list1;
    }
    //取小的尾插
    //为何这题可以直接定义一个尾结点?
    struct ListNode* newhead=NULL;
    struct ListNode* tail=NULL;
    while(list1 && list2)
    {
        if(list1->val <= list2->val)
        {
            if(newhead==NULL)
            {
                newhead=tail=list1;
                //tail=newhead->next;
            }
            else
            {
                tail->next=list1;
                //tail=list1->next;
                tail=tail->next;
            }
            list1=list1->next;
        }
        else
        {
            if(newhead==NULL)
            {
                tail=newhead=list2;
                //tail=newhead->next;
            }
            else
            {
                tail->next=list2;
                tail=tail->next;
            }
            list2=list2->next;
        }
    } 
    if(list1)//剩余直接尾插
    {
        //tail=list1;
        tail->next=list1;
    }  
    if(list2)
    {
        //tail=list2;
        tail->next=list2;
    }
    return newhead;
}    

Take care:

In the new chapter of data structure, there are more small details to pay attention to. It is best to consider all the situations that can be considered, otherwise it will be more troublesome to debug. Generally, only when the head node is empty, the direct assignment is equal to, and the rest generally need to be next linked.

Guess you like

Origin blog.csdn.net/hanwangyyds/article/details/132093211