1, Problem summary
2, Problem reply
2.1LeetCode: 2. Add two numbers
(1) Problem description
Here you are A non empty linked list that represents two non negative integers. Each of these numbers is based on Reverse order And each node can only store One Number.
Please add the two numbers and return a linked list representing sum in the same form.
You can assume that neither of these numbers will be 0 except the number 0 start.
Example 1:
Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807
Example 2:
Input: l1 = [0], l2 = [0]
Output: [0]
Example 3:
Input: L1 = [9,9,9,9,9,9], L2 = [9,9,9]
Output: [8,9,9,9,0,0,1]
(2) Problem analysis
This problem can define an auxiliary pointer. When the linked list is not NULL, define an integer t and add the values of two nodes respectively. Finally, divide and remainder are used to obtain the linked list.
(3) Code implementation
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode dummy=new ListNode(-1); ListNode pre=dummy; int t=0; while(t!=0||l1!=null||l2!=null){ if(l1!=null){ t+=l1.val; l1=l1.next; } if(l2!=null){ t+=l2.val; l2=l2.next; } pre.next=new ListNode(t%10); pre=pre.next; t=t/10; } return dummy.next; } }
2.2LeetCode:148. Sorting linked list
(1) Title Description:
Example 1:
Input: head = [4,2,1,3]
Output: [1,2,3,4]
Example 2:
Input: head = [-1,5,3,4,0]
Output: [- 1,0,3,4,5]
Example 3:
Input: head = []
Output: []
(3) Topic analysis
Before doing this problem, you can first look at the problem of merging two linked lists, so this idea can think in the direction of merging. Dividing a linked list into two linked lists and doing it according to the method of merging two linked lists can solve the problem. When using recursion in this problem, don't forget the end condition of recursion.
(4) Code implementation
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode sortList(ListNode head) { return sortList(head,null); } public ListNode sortList(ListNode head, ListNode tail) { if(head==null){ return head; }if(head.next==tail){ head.next=null; return head; } ListNode slow=head;ListNode fast=head; while(fast!=tail){ slow=slow.next; fast=fast.next; if(fast!=tail){ fast=fast.next; } } ListNode mid=slow; ListNode list1 = sortList(head,mid); ListNode list2 = sortList(mid,tail); ListNode sorted=merge(list1,list2); return sorted; } public ListNode merge(ListNode head1,ListNode head2){ ListNode dummyHead=new ListNode(0); ListNode temp=dummyHead,temp1=head1,temp2=head2; while(temp1!=null&&temp2!=null){ if(temp1.val<=temp2.val){ temp.next=temp1; temp1=temp1.next; }else{ temp.next=temp2; temp2=temp2.next; } temp=temp.next; } if(temp1!=null){ temp.next=temp1; }else if(temp2!=null){ temp.next=temp2; } return dummyHead.next; } }
2.3LeetCode:25. Turn over the linked list in groups of K
(1) Title Description
Give you a linked list, each k A group of nodes will be flipped. Please return to the flipped linked list.
k Is a positive integer whose value is less than or equal to the length of the linked list.
If the total number of nodes is not k If it is an integer multiple of, please keep the last remaining nodes in the original order.
Advanced:
Can you design an algorithm that uses only constant extra space to solve this problem?
You can't just change the value inside the node, but you need to actually exchange nodes.
Example 1:
Input: head = [1,2,3,4,5], k = 2
Output: [2,1,4,3,5]
Example 2:
Input: head = [1,2,3,4,5], k = 3
Output: [3,2,1,4,5]
Example 3:
Input: head = [1,2,3,4,5], k = 1
Output: [1,2,3,4,5]
Example 4:
Input: head = [1], k = 1
Output: [1]
(2) Topic analysis:
Before doing this problem, you should first reverse the linked list. This problem mainly uses a for loop to K nodes on the basis of reversing the linked list, and then reverse. If there are less than k left in the linked list, it will not be reversed. Here, the main attention is to the disconnection and connection of the linked list.
(3) Code implementation
/** * 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* reverse(ListNode* head){ ListNode temp=NULL; ListNode cur=head; while(cur!=nullptr){ ListNode* temp1=cur.next; cur.next=temp; temp=cur; cur=temp1; } return temp; } ListNode* reverseKGroup(ListNode* head, int k) { ListNode* dummy=new ListNode(0); dummy->next=head; ListNode* curr=dummy; ListNode* tail=dummy; ListNode* start,next; while(tail.next!=null){ for(int i=0;i<k&&tail!=null;i++){ tail=tail.next; } if(tail==nullptr){ break; } next=tail->next; start=pre->next; tail->next=NULL; pre->next=reverse(start); start->next=next; pre=start; tail=pre; } return curr->next; };
2.4LeetCode:445. Adding two numbers II
(1) Problem Description:
Give you two non empty linked lists 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.
You can assume that neither number starts with zero except the number 0.
Example 1:
Input: l1 = [7,2,4,3], l2 = [5,6,4]
Output: [7,8,0,7]
Example 2:
Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [8,0,7]
Example 3:
Input: l1 = [0], l2 = [0]
Output: [0]
(2) Problem analysis
Such reverse order problems are generally solved by stack. Create two stacks and put the two linked lists into the stack respectively. Finally, if the stack is not empty, pop up the elements, add them, and use division or remainder to obtain the reverse order linked list
(3) Code implementation
/** * 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* addTwoNumbers(ListNode* l1, ListNode* l2) { stack<int>s1,s2; while(l1!=NULL){ s1.push(l1->val); l1=l1->next; } while(l2!=NULL){ s2.push(l2->val); l2=l2->next; } ListNode* ans=NULL; int carry=0; while(!s1.empty()||carry!=0||!s2.empty()){ int a = s1.empty() ? 0 : s1.top(); int b = s2.empty() ? 0 : s2.top(); if (!s1.empty()) s1.pop(); if (!s2.empty()) s2.pop(); int cur=a+b+carry; carry=cur/10; cur%=10; ListNode* curnode=new ListNode(cur); curnode->next=ans; ans=curnode; } return ans; } };