runningtortoises的专栏

Problem:

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

Input:(2 -> 4 -> 3) + (5 -> 6 -> 4)Output:7 -> 0 -> 8

Solution:

题目大意:

给定两个单链表,每一个链表结点有一个0-9的数字,链表代表一个整数的倒序排列,,要求将两个链表相加并且结果也返回为和的倒序排列方式,例如Problem中的Input和Output

解题思路:

主要就是模拟大整数相加,注意进位问题和单链表的操作即可,题目简单,注意细节

Java源代码:

/** * Definition for singly-linked list. * public class ListNode { *int val; *ListNode next; *ListNode(int x) { val = x; } * } */public class Solution {public ListNode addTwoNumbers(ListNode l1, ListNode l2) {int high=(l1.val+l2.val)/10;ListNode head = new ListNode((l1.val+l2.val)%10);ListNode p=head;l1=l1.next;l2=l2.next;while(l1!=null|| l2!=null){int a =0 l1==null?0:l1.val;int b =0 l2==null?0:l2.val;ListNode s=new ListNode((a+b+high)%10);high = (a+b+high)/10;p.next=s;p=s;l1 = l1==null?null:l1.next;l2 = l2==null?null:l2.next;}if(high!=0){ListNode s=new ListNode(high);p.next=s;p=s;}return head;}}C语言源代码:

/** * Definition for singly-linked list. * struct ListNode { *int val; *struct ListNode *next; * }; */#include<stdio.h>#include<stdlib.h>struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {int high=(l1->val+l2->val)/10;struct ListNode* head = (struct ListNode*)malloc(sizeof(struct ListNode));struct ListNode* p=head;struct ListNode* s;head->val = (l1->val+l2->val)%10;head->next=NULL;l1=l1->next;l2=l2->next;while(l1!=NULL || l2!=NULL){int a = l1==NULL?0:l1->val;int b = l2==NULL?0:l2->val;s = (struct ListNode*)malloc(sizeof(struct ListNode));s->val = (a+b+high)%10;s->next=NULL;p->next=s;p=s;high = (a+b+high)/10;l1 = l1==NULL?NULL:l1->next;l2 = l2==NULL?NULL:l2->next;}if(high!=0){s = (struct ListNode*)malloc(sizeof(struct ListNode));s->val = high;s->next=NULL;p->next=s;}return head;}C++源代码:

/** * Definition for singly-linked list. * struct ListNode { *int val; *ListNode *next; *ListNode(int x) : val(x), next(NULL) {} * }; */#include<stdio.h>#include<stdlib.h>class Solution {public:ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {int high = (l1->val+l2->val)/10;struct ListNode* head = new ListNode((l1->val+l2->val)%10);head->next=NULL;struct ListNode* p;p=head;l1=l1->next;l2=l2->next;while(l1!=NULL || l2!=NULL){int a = l1==NULL?0:l1->val;int b = l2==NULL?0:l2->val;struct ListNode* s = new ListNode((a+b+high)%10);s->next=NULL;p->next=s;p=s;high = (a+b+high)/10;l1 = l1==NULL?NULL:l1->next;l2 = l2==NULL?NULL:l2->next;}if(high){struct ListNode* s = new ListNode(high);s->next=NULL;p->next=s;}return head;}};Python源代码:

# Definition for singly-linked list.# class ListNode:#def __init__(self, x):#self.val = x#self.next = Noneclass Solution:# @param {ListNode} l1# @param {ListNode} l2# @return {ListNode}def addTwoNumbers(self, l1, l2):high = (l1.val+l2.val)/10;head = ListNode((l1.val+l2.val)%10)p=head;l1=l1.nextl2=l2.nextwhile l1!=None or l2!=None:a =0 if l1==None else l1.valb =0 if l2==None else l2.vals = ListNode((a+b+high)%10)s.next=Nonep.next=sp=shigh = (a+b+high)/10l1 =None if l1==None else l1.nextl2 =None if l2==None else l2.nextif high!=0:s = ListNode(high)s.next=Nonep.next=sreturn head

如果你不出去走走,你就会以为这就是世界。

runningtortoises的专栏

相关文章:

你感兴趣的文章:

标签云: