[29] Reverse Linked List II | Ordered Linked List Conversion Binary Search Tree (LeetCode 92 | 109)

Reverse Linked List II

Problem Description

Reverse the linked list from position m to n. Please use one scan to complete the reversal.

Example:
Insert picture description here

Problem-solving ideas

Ideas seen in the comment area:

First locate the previous bit of the node to be reversed, and mark the predecessor node with the pre pointer; then scan the nodes to be reversed, and connect them to the back of the predecessor node one by one.

The reversal diagram of the example is as follows:
Insert picture description here

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
    
    
public:
    ListNode* reverseBetween(ListNode* head, int m, int n) {
    
    
        ListNode* pre_head = new ListNode(0,head);
        ListNode* pre = pre_head;
        for(int i = 1;i<m;i++)//将cur移动到要反转位置的前一个结点
            pre=pre->next;
        ListNode* cur = pre->next;//用head标记要反转的第一个节点
        for(int i = m; i < n; i++){
    
    //把要反转的结点逐个放到前驱节点的前面
            ListNode* temp = cur->next;
            cur->next = temp->next;
            temp->next = pre->next;
            pre->next = temp;
        }
        return pre_head->next;
    }
};

Time complexity: O(n)
Space complexity: O(1)

Ordered Linked List Conversion Binary Search Tree

Problem Description

Given a singly linked list, the elements in it are sorted in ascending order and converted into a highly balanced binary search tree.

In this question, a highly balanced binary tree means that the absolute value of the height difference between the left and right subtrees of each node of a binary tree does not exceed 1.

Problem-solving ideas

I have done a simple version of this kind of problem before- convert an ordered array into a binary search tree . The idea is to use the median as the root node, and the number on the left becomes its left subtree, and the tree on the right becomes its right subtree. tree. The same method is used to divide the left and right subtrees.

So this question can also use the same idea, the difficulty is how to find the median on the linked list.

Use the fast and slow pointers to find the median: the fast pointer and the slow pointer point to the head of the linked list at the same time, then the fast pointer advances two nodes at a time, and the slow pointer advances one node at a time. When the fast pointer reaches the end of the linked list, the slow pointer The position pointed to is the median node.

Code from the official solution:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
    
    
public:
    ListNode* getMedian(ListNode* left, ListNode* right) {
    
    //找中位数结点
        ListNode* fast = left;
        ListNode* slow = left;
        while (fast != right && fast->next != right) {
    
    
            fast = fast->next;
            fast = fast->next;
            slow = slow->next;
        }
        return slow;
    }

    TreeNode* buildTree(ListNode* left, ListNode* right) {
    
    
        if (left == right) {
    
    
            return nullptr;
        }
        ListNode* mid = getMedian(left, right);
        TreeNode* root = new TreeNode(mid->val);
        root->left = buildTree(left, mid);
        root->right = buildTree(mid->next, right);
        return root;
    }

    TreeNode* sortedListToBST(ListNode* head) {
    
    
        return buildTree(head, nullptr);//将表头和表尾作为参数传进去
    }
};

Guess you like

Origin blog.csdn.net/qq_43424037/article/details/113530550