Skip to content
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

Create add1.py #381

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions add1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Python program to add one to a linked list

class Node:
def __init__(self, data):
self.data = data
self.next = None

# Recursively add 1 from end to beginning and return
# carry after all nodes are processed.
def addWithCarry(head):

# If linked list is empty, return carry
if head is None:
return 1

# Add carry returned by the next node call
res = head.data + addWithCarry(head.next)

# Update data and return new carry
head.data = res % 10
return res // 10

def addOne(head):

# Add 1 to linked list from end to beginning
carry = addWithCarry(head)

# If there is carry after updating all nodes,
# then we need to add a new node to the linked list
if carry:
newNode = Node(carry)
newNode.next = head

# New node becomes head now
return newNode

return head

def printList(head):
curr = head
while curr:
print(curr.data, end=" ")
curr = curr.next
print()

if __name__ == "__main__":

# Create a hard-coded linked list:
# 1 -> 9 -> 9 -> 9
head = Node(1)
head.next = Node(9)
head.next.next = Node(9)
head.next.next.next = Node(9)

head = addOne(head)

printList(head)