New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
additon of integers using LL #2370
Open
csendranshi
wants to merge
2
commits into
TheAlgorithms:master
from
csendranshi:addNodes
base: master
+81
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.
| @@ -0,0 +1,81 @@ | ||
| """Adding two integers of same length using Linked lists | ||
| Example add 321 + 248 = 569 | ||
| (1)->(2)->(3) + (8)->(4)->(2) = (5)->(6)->(9) """ | ||
|
|
||
|
|
||
| # node definition | ||
| class Node: | ||
| def __init__(self, data): | ||
|
||
| self.data = data | ||
| self.next = None | ||
| # self.head=None | ||
|
|
||
|
|
||
| # linkedlist definition | ||
| class LL: | ||
| def __init__(self): | ||
| self.head = None | ||
|
|
||
| # method to print the linked list | ||
| def printLL(self): | ||
cclauss
Member
|
||
| temp = self.head | ||
| while temp: | ||
| print(temp.data, end=" ") | ||
| temp = temp.next | ||
|
|
||
| # method to push a new node onto the linked list | ||
| def push(self, newdata): | ||
cclauss
Member
|
||
| newnode = Node(newdata) | ||
| newnode.next = self.head | ||
| self.head = newnode | ||
|
|
||
| # method to add the nodes | ||
| def add(self): | ||
| carry = [] # to store carry generated if any | ||
| carry.append(0) | ||
| temp1 = llist.head | ||
| temp2 = ll2.head | ||
| while temp1 and temp2: | ||
| sums = ( | ||
| temp1.data + temp2.data + carry[-1] | ||
| ) # adding the latest element appended to the carry list | ||
| if sums >= 0 and sums <= 9: | ||
| res.push(sums) | ||
| carry.append(0) | ||
| else: | ||
| res.push(sums % 10) | ||
| carry.append(int(sums / 10)) | ||
|
|
||
| temp1 = temp1.next | ||
| temp2 = temp2.next | ||
|
|
||
| if carry[-1] != 0: # if carry is generated : nonzero | ||
| res.push(carry[-1]) # add it to the res list | ||
|
|
||
|
|
||
| llist = LL() # linkedlist 1 | ||
| ll2 = LL() # linkedlist 2 | ||
| res = LL() # linkedlist representing addition of the nodes taken as an integer | ||
|
|
||
|
|
||
| # adding 321 and 248 | ||
| llist.push(3) | ||
| llist.push(2) | ||
| llist.push(1) | ||
|
|
||
| ll2.push(2) | ||
| ll2.push(4) | ||
| ll2.push(8) | ||
|
|
||
| # Test case for carry generation handling | ||
| # llist.push(9) | ||
| # llist.push(9) | ||
| # llist.push(9) | ||
| # ll2.push(0) | ||
| # ll2.push(0) | ||
| # ll2.push(1) | ||
|
|
||
| llist.add() | ||
| res.printLL() | ||
ProTip!
Use n and p to navigate between commits in a pull request.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.


Please add Python type hints for function parameters as discussed in CONTRIBUTING.md.