Title:
Two non-empty linked lists are given to represent two non-negative integers. Among them, their respective digits are stored in reverse order, and each node of them can only store one digit.
If we add the two numbers together, we return a new list to represent their sum.
You can assume that neither of these numbers will begin with zero except the number 0.
Example:
Input: (2 - > 4 - > 3) + (5 - > 6 - > 4)
Output: 7 - > 0 - > 8
Cause: 342 + 465 = 807
nums1=list(int(x) for x in input().split()) nums2=list(int(x) for x in input().split()) class ListNode(): def __init__(self,x): self.value=x self.next=None def __str__(self): return str(self.value) def print_listnode(listnode): """ //Print list """ res=[] head=listnode temp=head while temp: res.append(temp.value) temp=temp.next print(res) def list_to_link(nums): """ //Converting from List to Linked List """ new=ListNode(0) head=new for num in nums: head.next=ListNode(num) head=head.next #return print_listnode(new.next) return new.next #print(list_to_link(nums)) def link_to_list(listnode): """ //Converting linked lists into lists :param listnode: :return: """ res=[] head=listnode temp=head while temp!=None: res.append(temp.value) temp=temp.next return res def link_to_int(listnode): """ //Converting linked lists into shaping :param listnode: :return: """ str_int=''#Create an empty string head=listnode temp=head while temp!=None: str_int=str_int+str(temp.value) temp=temp.next return int(str_int)#Conversion to integer def int_to_list(int_num): """ //Convert int to list """ #list_int=map(int,str(int_num)) list_int=[int(x) for x in str(int_num)] return list(list_int) def list_to_int(lists): """ //Converting lists to integers """ res='' for list in lists: res=res+str(list) return int(res) def int_reserve_to_link(int_num): """ //Inverse order of integers into a linked list """ value=ListNode(int_num%10) link3=value#Output Link List int_num=int_num//10 while int_num!=0: link3.next=ListNode(int_num%10) int_num=int_num//10 return value def int_to_link(int_num): """ //Turn the order of integers into a linked list //First it becomes a list, then a linked list. """ list_nums1=link_to_list(list_to_link(nums1)) list_nums2=link_to_list(list_to_link(nums2)) #print(list_nums1) reverse_list_nums1s=list(reversed(list_nums1)) reverse_list_nums2s=list(reversed(list_nums2)) #print(reverse_list_nums1s) int_nums1=list_to_int(reverse_list_nums1s) int_nums2=list_to_int(reverse_list_nums2s) int_nums3=int_nums1+int_nums2 #print(int_nums3) list_nums3=int_to_list(int_nums3) #print(list_nums3) reverse_list_nums3=list(reversed(list_nums3)) #print(reverse_list_nums3) link_nums3=list_to_link(reverse_list_nums3) print_listnode(link_nums3) #link_nums3=int_reserve_to_link(int_nums3) #print(link_nums3) #print(link_nums3.next) #print_listnode(link_nums3)