leetcode 206. 反转链表
题目
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f82417a8f5a7a405e65aa0f93a99e3ba.png)
class Solution {
public:
ListNode* dfs(ListNode* head,ListNode* pre){
ListNode *nex=head->next;
head->next=pre;
if(!nex) return head;
return dfs(nex,head);
}
ListNode* reverseList(ListNode* head) {
if(!head) return NULL;
return dfs(head,NULL);
}
};
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(!head) return head;
ListNode *now=head,*nex=head->next,*nexnex;
while(nex) nexnex=nex->next,nex->next=now,now=nex,nex=nexnex;
head->next=NULL;
return now;
}
};
leetcode 2. 两数相加
题目
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8c32cb7ecc0b6d0e6a6ba7d3476cd6ad.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9911971751b05299ddd32d9870c06473.png)
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *head=l1,*tmp=NULL;int las=0;
while(l1&&l2){
int x=l1->val,y=l2->val;
l1->val=(x+y+las)%10,las=(x+y+las)/10;
tmp=l1,l1=l1->next,l2=l2->next;
}
if(tmp==NULL) return l2;
if(l1==NULL) tmp->next=l2,l1=l2;
while(l1&&las){
int x=l1->val;
l1->val=(x+las)%10,las=(x+las)/10;
tmp=l1,l1=l1->next;
}
if(las) tmp->next=new ListNode(1);
return head;
}
};