I have to Convert a number into linked list such that each digit is in a node and pointing to node having next digit.The function should return head of the linked list. e.g input 120 should create a list of Nodes 1 -> 2 -> 0 and return the reference.If it is a negative number say -120 it should return -1->-2->0.I tried to do it in this way:
def number_to_list(number):
head,tail = None,None
for x in str(number):
if x<0:
x = -int(x)
node = Node(int(x))
else:
node = Node(int(x))
if head:
tail.next = node
else:
head = node
tail = node
return head
pass
It is working fine for positive numbers but if I pass -120.It is showing an error:
ValueError: invalid literal for int() with base 10: '-'.
How can I fix it.
int()on each, you'll need to special-case "-", because it's not an integer.