Starting with the C-language pointer, the path to my algorithm is over!
Today in order to find a good internship, had to pick up, wrote three years of the web, the algorithm dropped too much
Today, on the Leetcode, it is difficult to write on a simple list, so record
0 , these two numbers do not start with 0.
Example: input: (2,4, 3) + (5, 6, 4) output:7, 0, 8 Reason:342 + 465 = 80 7
The problem-solving process is several twists and turns, the first to get out of the answer is still the first plug-in, and also timed out, the real food
/*** Definition for singly-linked list. * public class ListNode {* int val; * ListNode Next; * ListNode (int X) {val = x;}}*/classSolution { PublicListNode addtwonumbers (listnode L1, ListNode L2) {intflag=0;//is used to determine if carry 1 is carry, 0 is not in, and can be stored directly.
int sum=0and//for storing the sum of the current bits andintI1=0; Val of L1intI2=0; Val ListNode head of L2=NewListNode (0); Declares a node as the head node, because it is a tail plug, and the final node is the last node value of the Nodes ListNode node=Head; Declares a temporary variable that is used for the operation of the tail plug while(l1!=NULL|| l2!=NULL){
If the current node is empty, it is assigned a value of 0 to facilitate continuation of the operationil = (L1!=null)? l1.val:0;if(l2!=NULL) {I2=L2.val; }Else{i2=0; } Sum=i1+i2+Flag; Get the current operation's and flag=sum/10; Whether rounding
Inserts a node at the end of the current node, storing the value of the current node for the first operation, the equivalent of Head.next=new ListNode (7) which is why the final return to Head.next Node.next=NewListNode (sum%10);
Assigns the next value of the current node to the current node, moving the pointer to the tail node of the linked list=Node.next; if(L1! =NULL) L1 =L1.next; if(L2! =NULL) L2 =L2.next; }
If the final carry, then insert a new node to the tailif(Flag > 0) {Node.next=Newlistnode (flag); } returnHead.next; }}
In the subject, the use of the tail interpolation method, the end of the list is inserted into the new node
When you take a value, you only need to take a downward value on the first head
Input: (2, 4, 3) + (5, 6, 4) output:7, 0, 8 reason:342 + 465 = 807
0. Before starting the loop
head={ 0 null}node={ 0 null }
1. First cycle
node.next=new ListNode (sum%10 ); Run Result:node={val:0Next: {val:7Next:NULL}} because the head=Node:head={val:0Next: {val:7Next:NULL}}node = node.next; run Result:node={val:7Next:NULL;} The head does not change2. Second cycle
node.next=new ListNode (sum%10 ); Run Result:node={val:7Next: {val:0Next:NULL}} because the head=Node:head={val:0Next: {val:7Next: {val:0Next:NULL}}} node = node.next; run Result:node={val:0Next:NULL;} The head does not change3. Third Cycle
node.next=new ListNode (sum%10 ); Run Result:node={val:0Next: {val:8Next:NULL}} because the head=Node:head={val:0Next: {val:7Next: {val:0Next: {val:8Next:NULL}}}} node = node.next; run Result:node={val:8Next:NULL;} The head does not change4. Return results
head={ 0 next : {7 next: {0 next : {8 NULL } } }}head.next={ 7 Next: { 0 Next: { 8 null }
5. Summary
The first head node is the entire operation, the node is the equivalent of a tail, constantly adding data to itself, and then moving back to the added node
Similar to a snake, the head node is constant, and node node is a working node (length 1)
His job is to find a new node and attach it to his next (Node.next=new ListNode)
And move on to this new node (Node=node.next)
Repeat the work.
Beginner's algorithm Getting started: A list of Java operations