首页 > 代码库 > leetcode Add Two Numbers(对指针的一些基本操作)

leetcode Add Two Numbers(对指针的一些基本操作)

 1      ListNode *ptr,*l;
 2     l = new ListNode(0);//这才是正确的赋值姿势
 3     ptr = l;//赋给的是地址
 4     int up = 0,fg1 = 0,fg2 = 0;
 5     //cout<<"r"<<endl;
 6     while(1)
 7     {
 8         if(fg1 && fg2) break;
 9         int a,b;
10         if(fg1) a = 0;
11         else a = l1 -> val;
12         if(fg2) b = 0;
13         else b = l2 -> val;
14        // cout<<a<<" "<<b<<endl;
15         l -> next = new ListNode( (a + b + up) % 10);
16         if(a + b + up >= 10) up = 1;
17         else up = 0;
18         if(!(l1 -> next)) fg1 = 1;
19         else    l1 = l1 -> next;
20         if(!(l2 -> next)) fg2 = 1;
21         else    l2 = l2 -> next;
22         l = l -> next;
23     }
24     if(up) l -> next  = new ListNode(up);
25         return ptr -> next;

 

leetcode Add Two Numbers(对指针的一些基本操作)