A similar problem in the LeetCode question bank about finding the intermediate nodes of the linked list

1. The middle node of the linked list

Title description:
Given a non-empty singly linked list whose head node is head, return the middle node of the linked list.

If there are two intermediate nodes, return to the second intermediate node.
Insert picture description here
This is a problem that belongs to the problem-solving type of a type of fast and slow pointer.

//可以定义两个指针,一个每次走两步,一个每次只走一步,你就会发现,当快指针走到结尾的时候,慢指针刚好走到中间
struct ListNode* middleNode(struct ListNode* head)
{
    
    
    struct ListNode* slow =head;
     struct ListNode* fast =head;
     while(fast && fast->next) 
     //循环想的是结束的条件但是写的确实继续下去的条件,所以对于中间这个&&,是需要同时满足奇数个数组和偶数个数组的条件
     {
    
    
         slow = slow->next;
         fast = fast->next->next;
     }
     return slow;
}

Insert picture description here

two.

Title description:

Input a linked list and output the kth node from the bottom of the linked list.

Problem-solving idea: define two pointers, slow and fast, let fast take k steps first, and then the two pointers go together, then the difference between them is always K, and then when fast is NULL, it is an unending sign , The slow at this time is the Kth node from the bottom to be found

class Solution {
    
    
public:
    ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
    
    
    ListNode* slow = pListHead,* fast = pListHead;
    //你这里的K值有可能比你链表本身的长度长,所以要保证其实有效的,
    //这一题最不好想到的就是这里。
        while(k--)
        {
    
    
            if(fast != NULL)
                fast = fast->next;
            else
                return NULL;
        }
        while(fast)
        {
    
    
            slow = slow->next;
            fast = fast->next;
        }
        return slow;
    }
};

Insert picture description here

Niuke.com-palindrome structure of linked list

Title description:
For a linked list, please design an algorithm with a time complexity of O(n) and an additional space complexity of O(1) to determine whether it is a palindrome structure.

Given the head pointer A of a linked list, please return a bool value indicating whether it is a palindrome. Ensure that the length of the linked list is less than or equal to 900.

Test example:
1->2->2->1
Return: true

What is a palindrome?
That list is symmetry in

Problem-solving idea : This problem cleverly uses the methods we have learned and used, fast and slow pointer + inverse linked list method to solve this problem
//1->2->3->3->2->1 For The even number of slows just walked to his lower median
//1->2->3->2->1 For the odd number of slows just walked to the middle
①Find the node that is symmetrical in the middle ②Flip the
second half of the linked list
③ Compare whether the two linked lists are equal ( note : for the last node of the linked list in the first half, the next one points to the address of the original node, but for the last node in the second half Is NULL, so the next address pointed to by the last node in the previous half should be set to NULL)

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};*/
class PalindromeList {
    
    
public:
    bool chkPalindrome(ListNode* A) {
    
    
        //①找出中间的那个结点
        ListNode* slow = A;
        ListNode* fast = A;
        ListNode* prev = NULL;
        //在下面逆置的时候需要知道slow的上一个结点的位置
        while(fast && fast->next)
        {
    
    
            prev = slow;
            slow = slow->next;
            fast = fast->next->next;
        }
        //这一步就是所写的“注意”
        prev->next = NULL;
        //②逆置链表
        ListNode* newHead = NULL;
        ListNode* cur = slow;
        while(cur)
        {
    
    
            ListNode* next = cur->next;
            cur->next = newHead;
            newHead = cur;
            cur = next;
        }
        //③比较(此时你使用A或者newhead都是可以的)
        while(A)
        {
    
    
            if(newHead->val != A->val)
            {
    
    
                return false;
            }
            else
            {
    
    
                newHead = newHead->next;
                A = A->next;
            }
        }
        return true;
    }
};

Guess you like

Origin blog.csdn.net/MEANSWER/article/details/111871929