LeetCode brush title (xvi) the list ----- ------- easy part (Java, C ++)

237. delete the list of nodes

请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点。
现有一个链表 -- head = [4,5,1,9],它可以表示为:

Here Insert Picture Description
Example 1:
Input: head = [4,5,1,9], node = 5
Output: [4,1,9]
Explanation: You list whose value is given the second node 5, then call you after the function of the strain list 4 -> 1 -> 9.

Example 2:
Input: head = [4,5,1,9], node = 1
Output: [4,5,9]
Explanation: You list whose value is given a third node 1, then the call you after the function of the strain list 4 -> 5 -> 9.
Description:
• list comprising at least two nodes.
• values of all the nodes in the list are unique.
• a non-given node and the end node must have a valid node in the linked list.
• Do not return any results from your functions.

A thought:
Method: exchange with the next node
from the list To remove a node node The most common method is to modify the next pointer node before the node after it is pointing.
Here Insert Picture Description
Because we can not access node before the node we want to delete, we always can not modify the next pointer of the node. Instead, we must value of the node you want to remove it later replaced by the value of the node, and then delete the node after it.
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Because we know that the end of the node to be removed is not a list, so we can ensure that this approach is feasible.
Here Insert Picture Description
Complexity analysis
time and space complexity is: O (1).
Link: https: //leetcode-cn.com/problems/delete-node-in-a-linked-list/solution/shan-chu-lian-biao-zhong-de-jie-dian-by-leetcode/
idea of two :
stand-attack
Here Insert Picture Description
link: https: //leetcode-cn.com/problems/delete-node-in-a-linked-list/solution/ti-shen-gong-ji-by-vailing/
me:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    void deleteNode(ListNode* node) 
    {
        node -> val = node -> next -> val;
        node -> next = node -> next -> next;   
    }
};

206. reversal list

反转一个单链表。
示例:
输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
进阶:
你可以迭代或递归地反转链表。你能否用两种方法解决这道题?

A thought:
Method One: iterative
assumed that there is a linked list 1 → 2 → 3 → Ø, we want to put it into Ø ← 1 ← 2 ← 3.
When traversing the list, the next pointer of the current node to point to the previous element. Since the node which is not a reference node, which must be stored in advance before the element. Prior to change the reference, the other pointers needed to store the next node. Do not forget to return the new head of reference at the end!
Here Insert Picture Description
Complexity Analysis
• time complexity: O (n), assuming n is the length of the list, the time complexity is O (n).
• space complexity: O (1).
Method Two: recursive
recursive version is slightly more complicated, the key is to work in reverse. Assuming that the rest of the list has been reversed, now how do I reverse it in front of the part?
List is assumed that:
Here Insert Picture Description
if the node n_ {k + 1} to n_ {m} has been inverted, while we are at n_ {k}.
Here Insert Picture Description
We hope n_ {k + 1} for the next node pointing n_ {k}.
Therefore, n_ {k} .next.next = n_ {k}.
Be careful that n_ {1} to be the next point Ø. If you ignore this, your list may be generated in the cycle. If you use the list as a test code size 2, you might catch this error.
Here Insert Picture Description
Complexity Analysis
time complexity: O (n), nn is assumed length of the list, then the time complexity is O (n).
Complexity Space: O (n), the use of recursion, stack space will be implicit. Recursion depth may reach the n-layer.

Author: LeetCode
link: https: //leetcode-cn.com/problems/reverse-linked-list/solution/fan-zhuan-lian-biao-by-leetcode/
Source: stay button (LeetCode)
copyright reserved by the authors. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.
Thinking two:
Iteration:
Here Insert Picture Description
Here Insert Picture Description
recursion:
Here Insert Picture Description
My:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) 
    {
        if(!head)
        {
            return nullptr;
        }    
        ListNode* first = head;
        ListNode* target = head -> next;
        while(target != nullptr)
        {
            first -> next = target -> next;
            ListNode* temp = target -> next;
            target -> next = head;
            head = target;
            target = temp;
        }
        return head;
    }
};

21. The combined two ordered lists

将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 
示例:
输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4

A thought:
Method 1: Recursive
idea
we can recursively defined as follows in the two lists in the merge operations (ignoring border situations, such as empty list, etc.):
Here Insert Picture Description
In other words, two lists with a smaller head rest element the merge operation merge the results.
algorithm

We will direct more than a recursive process modeling, first consider the boundary conditions.

Special, if l1 or l2 beginning null, then there is no need to merge operations, so we only need to return a non-empty list. Otherwise, we have to determine which l1 and l2 a smaller element of the head, then down a decision recursively added to the result in value. If the two lists are empty, then the process terminates, so recursive process will eventually terminate.
Here Insert Picture Description
Complexity Analysis
time complexity: O (n + m). Because the recursive will remove the first element of l1 or l2 (until at least one list is empty), the function mergeTwoList only once through each element of each call. Therefore, the length of the list after the time complexity combined with a linear relationship.
Space complexity: O (n + m). When calling mergeTwoLists exit l1 and l2, where each element must have been traversed, so the stack frames n + m consumes space O (n + m) is.

Method 2: iterative
idea
we can use an iterative approach to achieve the above algorithm. We assume that l1 elements are strictly less than l2 elements, we can l2 l1 insert the elements one by one in the correct position.
Algorithm
First, we set a sentinel node "prehead", which allows us to more easily return at the end of the list after the merger. We maintain a prev pointer, we need to do is adjust its next pointer. Then, we repeat the process until the point l1 or l2 null: if the value of the current position is less than or equal l1 l2, l1 we put the value in the access node behind the prev pointer moves back while a l1. Otherwise, we do the same for l2. Whether we will pick which element in the back, we regard prev move back one element.

When the loop terminates, l1 and l2 at most a non-empty. Since all the elements of two lists inputs are ordered, so no matter what the list is non-empty, it contains all the elements than previously merged linked list is bigger. This means that we simply need to be connected to a non-empty list behind the merger of the list, and returns the combined list.
Here Insert Picture Description
Complexity Analysis
time complexity: O (n + m). Because each loop iteration, only one element L1 and l2 will be merged into the list, while the number of times equal to the total length of two cycles of the list. All other work is a constant level, so the total time complexity is linear.

Complexity Space: O (1). Iterative process will only produce a few pointers, so the space it needs is a constant level.

Author: LeetCode
link: https: //leetcode-cn.com/problems/merge-two-sorted-lists/solution/he-bing-liang-ge-you-xu-lian-biao-by-leetcode/
Source: force buckle (LeetCode)
copyright reserved by the authors. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

Thinking II:
Method 1: Recursive
Here Insert Picture Description
Method 2: iterative
Here Insert Picture Description
link: https: //leetcode-cn.com/problems/merge-two-sorted-lists/solution/he-bing-liang-ge-you-xu-lian-biao -guan-fang-cban-b /
I:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) 
    {
        if(l1 == NULL)
        {
            return l2;
        }
        else if(l2 == NULL)
        {
            return l1;
        }  
        else if(l1->val <= l2-> val)
        {
            l1 -> next = mergeTwoLists(l1->next,l2);
            return l1;
        }
        else
        {
            l2 -> next = mergeTwoLists(l1,l2->next);
            return l2;
        }
    }
};

Published 47 original articles · won praise 21 · views 7227

Guess you like

Origin blog.csdn.net/qq_18315295/article/details/103773548