Click here to Skip to main content
15,506,846 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Given a number in the form of a linked list(each node contains a digit) you have to break it into two halves, containing floor(n/2) and ceil(n/2) digits respectively. You have to return a pointer to the linked list containing the sum of the numbers represented by the two linked lists.

INPUT: First line contains an integer n. Second line contains n space separated digits.

OUTPUT: A pointer to the sum of the linked lists(should not contain any leading zeroes.(The print List prints all elements in the linked list provided as an argument). )

EXAMPLE: INPUT: 6 1 2 3 4 5 6

OUTPUT: 5->7->9

Explanation: The two linked list will be (1->2->3) and (4->5->6). So we will do an addition 123+456 to get 579

INPUT: 5 0 1 0 1 2

OUTPUT: 1->3

Explanation: The two linked list will be (0->1) and (0->1->2). So we will do an addition 01+012 to get 013. Since no leading zeros is to be printed the output is 13.

What I have tried:

Not able to do it by linked list.
Updated 24-Feb-22 8:59am
jeron1 24-Feb-22 13:03pm    
What is the C question?
Patrice T 24-Feb-22 13:38pm    
It is a do my homework question.
jeron1 24-Feb-22 13:43pm    
I know (hence my vote), but for some reason, I am feeling somewhat optimistic today. A condition I am not used to, trying to remedy as we speak.
Andre Oosthuizen 24-Feb-22 15:31pm    
5 Stars for Griff for doing the effort of posting subtly to someone that we do NOT do homework here. :)
Rick York 24-Feb-22 20:55pm    
Do a search at the site of your preference for "linked list." You should find several million possibilities including a few at this site. That should tell you how to implement a linked list. This YOUR problem so you need to do the work.

1 solution

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900