[] [List] wins the Offer to merge two sorted lists

Title: two monotonically increasing input list and output list after synthesis of two lists, of course, after that we need to meet synthesis list - decreasing the rules.

 

A: If a list is empty, the list head node is combined pHead2; 2 if the list is empty, then the combined list head node is pHead1

   Create a new list as a combined ret_Head the first node, and comparing pHead1 pHead2, ret_Head small number of points, ret_Head-> next two points to the list of the next smallest number ====> Recursive

 

A: The same, but also a recursive stack structure, the stack can also be used to solve this problem

  

/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
    {
        if(pHead1 == nullptr)
        {
            return pHead2;
        }
        if(pHead2 == nullptr)
        {
            return pHead1;
        }
        ListNode *ret_Head = nullptr;
        
        if(pHead1->val < pHead2->val)
        {
            ret_Head = pHead1;
            ret_Head->next = Merge(pHead1->next,pHead2);
        }
        else
        {
            ret_Head = pHead2;
            ret_Head->next = Merge(pHead1,pHead2->next);
        }
        return ret_Head;
    }
};

  

 

Related topics:

  A set of reversed every K list: given a chain, each set of node k is flipped, and returns the list after inversion. k is a positive integer that is less than or equal to the length of the list. If the total number of nodes is not an integral multiple of k, then the node will last remaining original order.                          

             https://www.nowcoder.com/practice/a632ec91a4524773b8af8694a51109e7            
               Description:
            1. Do you need to define their own list structure, save the data to the input in your list;
            2. You can not just simply change the internal node values, but need to be the actual node exchange;
            3. You can use the extra space algorithm constants.

  Programming single chain reversal function: function to achieve reversal of a single linked list, a list input, the inverted list, return the list after the flip.

  LRU cache: Design a data structure, to achieve LRU Cache function (Least Recently Used - least recently used cache). It supports the following two operations: get and put.

        https://www.nowcoder.com/practice/3da4aeb1c76042f2bc70dbcb94513338

            int get (int key) - If the key already exists, a value corresponding to key value (always greater than 0) return; if the key does not exist, or -1.

         void put (int key, int value) - If the key does not exist, the value is inserted; if key already exists, replacing the value previously existing value is used. If the capacity limit is reached, LRU Cache needs before inserting a new element, the element least recently used deleted.

         Pay particular attention to the definition of "use": the newly inserted or acquire key deemed to be used once; and the value of replacing the already existing update, is not being used. Restrictions: complete the two operations at the time complexity of O (1) is.

Guess you like

Origin www.cnblogs.com/xiexinbei0318/p/11427295.html