Weekly summary: October 4, 2021 - October 10
Here are some questions that impressed me when I brushed the questions this week. Pick them out and make a summary
23. Merge K ascending linked lists
Give you a linked list array, each linked list has been arranged in ascending order.
Please merge all linked lists into an ascending linked list and return the merged linked list.
Example 1:
Input: lists = [[1,4,5],[1,3,4],[2,6]]
Output: [1,1,2,3,4,4,5,6]
Explanation: the linked list array is as follows:
[
1->4->5,
1->3->4,
2->6
]
Combine them into an ordered linked list.
1->1->2->3->4->4->5->6
hold 21. Merge two ordered linked lists Just take the method in, create a header node head, a pointer P = & head, use the for loop to traverse lists, pass p - > next and lists[i] to the function we set each time, then connect the returned linked list to lists, and finally return head.next.
/** * 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* sortList(ListNode *a,ListNode *b) { ListNode head,*p; p=&head; while(a!=NULL&&b!=NULL) { if(a->val>b->val) { p->next=b; p=p->next; b=b->next; }else { p->next=a; p=p->next; a=a->next; } } p->next=a!=NULL?a:b; return head.next; } ListNode* mergeKLists(vector<ListNode*>& lists) { ListNode head,*p; p=&head; for(int i=0;i<lists.size();i++) { p->next=sortList(p->next,lists[i]); } return head.next; } };
Sword finger Offer II 025. Add two numbers in the linked list
Two non empty linked lists l1 and l2 are given to represent two non negative integers. The highest digit is at the beginning of the linked list. Each of their nodes stores only one digit. Adding these two numbers will return a new linked list.
It can be assumed that neither number will start with zero except the number 0.
Example 1:
Input: l1 = [7,2,4,3], l2 = [5,6,4]
Output: [7,8,0,7]
Use the two linked lists directly 206. Reverse linked list Reverse the method, then start adding, take a number num to calculate whether the result is one, create a header node head, create a pointer P = & head, and then each addition result is made into a new node hanging behind P, and then p goes down one. When either of the two added linked lists is empty, end the traversal, and connect the rest directly behind P. if the node is hung up, But num is not zero in the end, so we have to make a node. val=num is hung behind P. finally, reverse the head and return to head.next.
/** * 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* reverseList(ListNode* head) { stack<ListNode*>sta; while(head!=NULL) { sta.push(head); head=head->next; } ListNode p,*q; q=&p; while(!sta.empty()) { q->next=sta.top(); sta.pop(); q=q->next; } q->next=NULL; return p.next; } };
Sword finger Offer II 022. The entry node of the link in the linked list
Given a linked list, return the first node from the linked list into the ring. Starting from the head node of the linked list, the first node entering the ring along the next pointer is the entry node of the ring. If the linked list is acyclic, null is returned.
In order to represent the rings in a given list, we use the integer pos to represent the position where the tail of the list is connected to the list (the index starts from 0). If pos is - 1, there is no ring in the linked list. Note that pos is only used to identify the ring and is not passed to the function as an argument.
Note: it is not allowed to modify the given linked list.
Example 1:
Input: head = [3,2,0,-4], pos = 1
Output: returns the linked list node with index 1
Explanation: there is a ring in the linked list, and its tail is connected to the second node.
Using the fast and slow pointers p and Q, p takes one step and Q takes two steps each time. When p and Q meet for the first time, p does not move and moves Q back to head. When p and Q meet again for the second time, the point of p is the first node at the ring. Of course, it is also possible that the linked list does not have a ring. At that time, q is directly null, and finally null is returned. If there is a ring, p is returned. As for why I wrote this, I said it in my punch in the other day. I won't repeat it here.
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *detectCycle(ListNode *head) { ListNode *fast = head, *slow = head; while (true) { if (fast == nullptr || fast->next == nullptr) return nullptr; fast = fast->next->next; slow = slow->next; if (fast == slow) break; } fast = head; while (slow != fast) { slow = slow->next; fast = fast->next; } return fast; } };
876. Intermediate node of linked list
Given a non empty single linked list with head node, return the intermediate node of the linked list.
If there are two intermediate nodes, the second intermediate node is returned.
Example 1:
Input: [1,2,3,4,5]
Output: node 3 in this list (serialized form: [3,4,5])
The returned node value is 3. (the serialization expression of this node in the evaluation system is [3,4,5]).
Note that we returned an object ans of ListNode type, as follows:
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, and ans.next.next.next = NULL
When writing this question for the first time, I first traverse the linked list, store the length of the linked list, then take the middle value, and then traverse from the beginning. When I traverse to the middle position, I return to that node, and I have to traverse both sides. Now I use the fast and slow pointers p and Q to traverse p one step and Q two steps at a time. When q is NULL, p is in the middle and returns p.
/** * 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* middleNode(ListNode* head) { ListNode *p,*q; p=head; q=head; while(q->next!=NULL) { p=p->next; if(q->next->next==NULL) { return p; } q=q->next->next; } return p; } };