Given two linked lists that represent two large positive numbers. The task is to subtract the given two numbers represented by the linked list. Subtract the smaller from the larger one.
Example 1:
Input:
L1 = 1->0->0
L2 = 1->2
Output: 8 8
Explanation:  12 subtracted from 100
gives us 88 as result.
Your Task:
The task is to complete the function subLinkedList() which should subtract the numbers represented by the linked list and return the head of the linked list representing the result.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= Length of the Linked List <= 10000

Source: geeksforgeeks | Difficulty: MEDIUM_HARD
Tags: Data structures

Starter code: #User function Template for python3

def subLinkedList(l1, l2): 
    # Code here
    # return head of difference list


Solutions:
def subLinkedList(l1, l2):
	u = []
	v = []
	while l1 != None:
		u.append(str(l1.data))
		l1 = l1.next
	while l2 != None:
		v.append(str(l2.data))
		l2 = l2.next
	r = ''.join(u)
	v = ''.join(v)
	j = max(int(r), int(v))
	e = min(int(r), int(v))
	s = j - e
	s = str(s)
	head = Node(s[0])
	t = head
	for i in range(1, len(s)):
		b = Node(s[i])
		head.next = b
		head = head.next
	return t


def subLinkedList(l1, l2):
	num1 = num2 = ''
	tmp = l1
	while tmp != None:
		num1 += str(tmp.data)
		tmp = tmp.next
	tmp = l2
	while tmp != None:
		num2 += str(tmp.data)
		tmp = tmp.next
	ans = list(str(abs(int(num1) - int(num2))))
	Head = LinkedList()
	for i in ans:
		Head.insert(i)
	return Head.head


def subLinkedList(l1, l2):
	temp1 = l1
	temp2 = l2
	str1 = ''
	str2 = ''
	while temp1 != None:
		str1 = str1 + str(temp1.data)
		temp1 = temp1.next
	while temp2 != None:
		str2 = str2 + str(temp2.data)
		temp2 = temp2.next
	result = str(abs(int(str1) - int(str2)))
	ls = LinkedList()
	for i in range(len(result)):
		ls.insert(int(result[i]))
	return ls.head


def subLinkedList(l1, l2):
	s1 = ''
	s2 = ''
	while l1:
		s1 += str(l1.data)
		l1 = l1.next
	while l2:
		s2 += str(l2.data)
		l2 = l2.next
	c = str(abs(int(s1) - int(s2)))
	a = []
	for i in c:
		a.append(i)
	head = Node(-1)
	tmp = head
	for i in a:
		node = Node(int(i))
		tmp.next = node
		tmp = tmp.next
	return head.next


def subLinkedList(l1, l2):

	def carry(a, i):
		a[i] -= 1
		if a[i] >= 0 or i <= 0:
			return
		else:
			a[i] = 9
			carry(a, i - 1)

	def cmpr(a, b):
		n1 = len(a)
		n2 = len(b)
		if n1 > n2:
			return (a, b)
		elif n1 < n2:
			return (b, a)
		else:
			for i in range(n1):
				if a[i] > b[i]:
					return (a, b)
				elif a[i] < b[i]:
					return (b, a)
			return (a, b)
	a = []
	b = []
	while l1:
		if not a and (not l1.data):
			pass
		else:
			a.append(l1.data)
		l1 = l1.next
	while l2:
		if not b and (not l2.data):
			pass
		else:
			b.append(l2.data)
		l2 = l2.next
	(p, q) = cmpr(a, b)
	n1 = len(p)
	n2 = len(q)
	c = [None] * n1
	if 1:
		i = 1
		while n2 - i >= 0:
			if p[n1 - i] >= q[n2 - i]:
				c[n1 - i] = p[n1 - i] - q[n2 - i]
			else:
				c[n1 - i] = p[n1 - i] + 10 - q[n2 - i]
				carry(p, n1 - i - 1)
			i += 1
		while n1 - i >= 0:
			c[n1 - i] = p[n1 - i]
			i += 1
	for i in range(n1):
		if c[i]:
			break
	head = Node(c[i])
	temp = head
	for j in range(i + 1, n1):
		head.next = Node(c[j])
		head = head.next
	return temp


def subLinkedList(l1, l2):
	p1 = []
	p2 = []
	cur1 = l1
	cur2 = l2
	while cur1:
		p1.append(cur1.data)
		cur1 = cur1.next
	while cur2:
		p2.append(cur2.data)
		cur2 = cur2.next
	s1 = ''.join(map(str, p1))
	s2 = ''.join(map(str, p2))
	i = abs(int(s1) - int(s2))
	i1 = str(i)
	k = []
	for j in i1:
		k.append(int(j))
	g = []
	for n in k:
		g.append(Node(n))
	g.append(None)
	head = cur = g[0]
	for v in range(1, len(g)):
		cur.next = g[v]
		cur = cur.next
	return head


def subLinkedList(l1, l2):

	def remove_zero(head):
		while head and head.data == 0:
			head = head.next
		return head

	def getlen(head):
		n = 0
		while head:
			n += 1
			head = head.next
		return n

	def reverse(head):
		if not head or not head.next:
			return head
		(prev, cur, nxt) = (None, head, head.next)
		while cur:
			cur.next = prev
			prev = cur
			cur = nxt
			nxt = cur.next if cur else None
		return prev
	l1 = remove_zero(l1)
	l2 = remove_zero(l2)
	len_l1 = getlen(l1)
	len_l2 = getlen(l2)
	if len_l2 > len_l1 or (len_l2 == len_l1 and l2.data > l1.data):
		(l1, l2) = (l2, l1)
	l1 = reverse(l1)
	l2 = reverse(l2)
	(ptr1, ptr2) = (l1, l2)
	carry = 0
	while ptr1:
		res = ptr1.data - carry
		res -= ptr2.data if ptr2 else 0
		if res < 0:
			res += 10
			carry = 1
		else:
			carry = 0
		ptr1.data = res
		ptr1 = ptr1.next
		ptr2 = ptr2.next if ptr2 else None
	l1 = reverse(l1)
	l1 = remove_zero(l1)
	return l1 if l1 else Node(0)


def subLinkedList(l1, l2):
	n = l1
	s1 = ''
	while n is not None:
		s1 = s1 + str(n.data)
		n = n.next
	n = l2
	s2 = ''
	while n is not None:
		s2 = s2 + str(n.data)
		n = n.next
	res = list(str(max(int(s1), int(s2)) - min(int(s1), int(s2))))
	if len(s1) >= len(s2):
		n = l1
		a = n
	else:
		n = l2
		a = n
	while n is not None:
		if res:
			n.data = int(res.pop(0))
			x = n
			n = n.next
		else:
			break
	x.next = None
	return a


def subLinkedList(l1, l2):
	il1 = 0
	il2 = 0
	while l1 or l2:
		if l1:
			il1 = il1 * 10 + l1.data
			l1 = l1.next
		if l2:
			il2 = il2 * 10 + l2.data
			l2 = l2.next
	res = str(abs(il1 - il2))
	a = Node(5)
	temp = a
	for i in res:
		temp.next = Node(int(i) % 10)
		temp = temp.next
	return a.next


def subLinkedList(l1, l2):
	itr1 = l1
	itr2 = l2
	s1 = ''
	s2 = ''
	while itr1:
		s1 += str(itr1.data)
		itr1 = itr1.next
	while itr2:
		s2 += str(itr2.data)
		itr2 = itr2.next
	s3 = str(abs(int(s1) - int(s2)))
	head = Node(s3[0])
	t = head
	for i in range(1, len(s3)):
		x = Node(s3[i])
		t.next = x
		t = t.next
	return head


def subLinkedList(l1, l2):
	a = l1
	b = l2
	(p, q) = ([], [])
	while a is not None:
		p.append(str(a.data))
		a = a.next
	while b is not None:
		q.append(str(b.data))
		b = b.next
	if int(''.join(p)) > int(''.join(q)):
		m1 = str(int(''.join(p)) - int(''.join(q)))
	else:
		m1 = str(int(''.join(q)) - int(''.join(p)))
	list1 = [int(i) for i in m1]
	head1 = None
	m = None
	for i in list1:
		p1 = Node(i)
		if head1 is None:
			head1 = p1
			m = head1
		else:
			head1.next = p1
			head1 = head1.next
	return m


def subLinkedList(l1, l2):

	def isGreater(n1, n2):
		count1 = 0
		count2 = 0
		(temp1, temp2) = (n1, n2)
		s1 = ''
		s2 = ''
		while temp1 != None:
			count1 = count1 + 1
			s1 = str(temp1.data) + s1
			temp1 = temp1.next
		while temp2 != None:
			count2 = count2 + 1
			s2 = str(temp2.data) + s2
			temp2 = temp2.next
		(temp1, temp2) = (n1, n2)
		if int(s1[::-1]) <= int(s2[::-1]):
			return False
		return True

	def rev(node):
		temp1 = node
		temp2 = node.next
		temp1.next = None
		while temp2 != None:
			y = temp2.next
			temp2.next = temp1
			temp1 = temp2
			temp2 = y
		return temp1

	def substract(n1, n2):
		temp1 = n1
		temp2 = n2
		carry = 0
		s = ''
		while temp1 != None and temp2 != None:
			subs = temp1.data - temp2.data - carry
			if subs >= 0:
				carry = 0
			if subs < 0:
				subs = subs + 10
				carry = 1
			s = str(subs) + s
			temp1 = temp1.next
			temp2 = temp2.next
		while temp1 != None:
			subs = temp1.data - carry
			if subs >= 0:
				carry = 0
			if subs < 0:
				subs = subs + 10
				carry = 1
			s = str(subs) + s
			temp1 = temp1.next
		s1 = ''
		flag = 0
		for i in s:
			if i == '0' and flag == 0:
				continue
			if i != '0' or flag == 1:
				flag = 1
				s1 = s1 + i
		return s1
	x = isGreater(l1, l2)
	if x == False:
		(l1, l2) = (l2, l1)
	(a, b) = (rev(l1), rev(l2))
	string = substract(a, b)
	if string == '':
		return Node('0')
	flag = 0
	for i in string:
		if flag == 0:
			head = Node(i)
			temp = head
			flag = 1
			continue
		temp.next = Node(i)
		temp = temp.next
	return head


def subLinkedList(l1, l2):
	l = []
	while l1:
		l.append(str(l1.data))
		l1 = l1.next
	s1 = ''.join(l)
	l.clear()
	while l2:
		l.append(str(l2.data))
		l2 = l2.next
	s2 = ''.join(l)
	l.clear()
	ans = abs(int(s1) - int(s2))
	ans = str(ans)
	l = list(ans)
	ll = LinkedList()
	for i in l:
		ll.insert(i)
	return ll.head


def subLinkedList(l1, l2):
	curr = l1
	temp = l2
	(string1, string2) = ('', '')
	while curr:
		string1 += str(curr.data)
		curr = curr.next
	while temp:
		string2 += str(temp.data)
		temp = temp.next
	(string1, string2) = (int(string1), int(string2))
	if string1 > string2:
		string1 = str(string1 - string2)
	else:
		string1 = str(string2 - string1)
	dummy = Node(-1)
	x = 0
	n1 = dummy
	while x < len(string1):
		newnode = Node(int(string1[x]))
		n1.next = newnode
		n1 = n1.next
		x += 1
	return dummy.next


def length(n):
	ret = 0
	while n:
		ret += 1
		n = n.next
	return ret

def reverse(head):
	prev = None
	current = head
	next = None
	while current is not None:
		next = current.next
		current.next = prev
		prev = current
		current = next
	return prev

def subLinkedList(l1, l2):
	while l1 is not None and l1.data == 0:
		l1 = l1.next
	while l2 is not None and l2.data == 0:
		l2 = l2.next
	n1 = length(l1)
	n2 = length(l2)
	if n2 > n1:
		(l1, l2) = (l2, l1)
	if n1 == n2:
		t1 = l1
		t2 = l2
		while t1.data == t2.data:
			t1 = t1.next
			t2 = t2.next
			if t1 is None:
				return Node(0)
		if t2.data > t1.data:
			(l1, l2) = (l2, l1)
	l1 = reverse(l1)
	l2 = reverse(l2)
	res = None
	t1 = l1
	t2 = l2
	while t1 is not None:
		small = 0
		if t2 is not None:
			small = t2.data
		if t1.data < small:
			t1.next.data -= 1
			t1.data += 10
		n = Node(t1.data - small)
		n.next = res
		res = n
		t1 = t1.next
		if t2 is not None:
			t2 = t2.next
	while res.next is not None and res.data == 0:
		res = res.next
	return res


def subLinkedList(l1, l2):
	no1 = ''
	no2 = ''
	temp = l1
	while temp != None:
		no1 += str(temp.data)
		temp = temp.next
	temp = l2
	while temp != None:
		no2 += str(temp.data)
		temp = temp.next
	res = str(abs(int(no1) - int(no2)))
	for i in range(len(res)):
		print(res[i], end=' ')


def subLinkedList(l1, l2):
	temp = l1
	(s1, s2) = (0, 0)
	while temp:
		s1 = s1 * 10 + temp.data
		temp = temp.next
	temp = l2
	while temp:
		s2 = s2 * 10 + temp.data
		temp = temp.next
	t = str(abs(s1 - s2))
	ans = Node(0)
	res = ans
	for i in t:
		ans.next = Node(int(i))
		ans = ans.next
	return res.next


def subLinkedList(l1, l2):
	h1 = l1
	h2 = l2
	l = ''
	l3 = ''
	while h1:
		l += str(h1.data)
		h1 = h1.next
	while h2:
		l3 += str(h2.data)
		h2 = h2.next
	l = int(l)
	l3 = int(l3)
	d = abs(l - l3)
	a = b = Node(-1)
	d = str(d)
	for i in d:
		a.next = Node(int(i))
		a = a.next
	return b.next


def subLinkedList(l1, l2):
	s1 = ''
	s2 = ''
	while l1:
		s1 += str(l1.data)
		l1 = l1.next
	while l2:
		s2 += str(l2.data)
		l2 = l2.next
	ans = abs(int(s1) - int(s2))
	d = Node(0)
	curr = d
	for digit in str(ans):
		curr.next = Node(int(digit))
		curr = curr.next
	return d.next


def subLinkedList(l1, l2):
	s1 = ''
	s2 = ''
	temp = l1
	while temp:
		s1 += str(temp.data)
		temp = temp.next
	temp = l2
	while temp:
		s2 += str(temp.data)
		temp = temp.next
	result = abs(int(s1) - int(s2))
	head = LinkedList()
	for i in str(result):
		head.insert(i)
	return head.head


def getNumber(l):
	num = 0
	p = l
	while p:
		num = num * 10 + p.data
		p = p.next
	return num

def subLinkedList(l1, l2):
	a1 = getNumber(l1)
	a2 = getNumber(l2)
	a = abs(a1 - a2)
	arr = map(int, str(a))
	ans = LinkedList()
	for i in arr:
		ans.insert(i)
	return ans.head


def subLinkedList(l1, l2):
	(diff1, diff2) = (0, 0)
	while l1:
		diff1 = diff1 * 10 + l1.data
		l1 = l1.next
	while l2:
		diff2 = diff2 * 10 + l2.data
		l2 = l2.next
	diff = diff1 - diff2
	if diff < 0:
		diff = -diff
	temp = Node(0)
	curr = temp
	for i in str(diff):
		curr.next = Node(i)
		curr = curr.next
	return temp.next


def subLinkedList(l1, l2):
	lst1 = []
	lst2 = []
	while l1 is not None:
		lst1.append(str(l1.data))
		l1 = l1.next
	while l2 is not None:
		lst2.append(str(l2.data))
		l2 = l2.next
	n1 = ''
	for ele in lst1:
		n1 = n1 + ele
	n2 = ''
	for ele in lst2:
		n2 = n2 + ele
	c = str(abs(int(n1) - int(n2)))
	k = []
	for ele in c:
		k.append(ele)
	k.append(-1)
	head = None
	tail = None
	for i in k:
		newnode = Node(i)
		if i != -1:
			if head is None:
				head = newnode
				tail = newnode
			else:
				tail.next = newnode
				tail = newnode
	return head


def subLinkedList(l1, l2):
	L1 = 0
	temp = l1
	val1 = ''
	while temp:
		val1 += str(temp.data)
		L1 += 1
		temp = temp.next
	L2 = 0
	temp = l2
	val2 = ''
	while temp:
		val2 += str(temp.data)
		L2 += 1
		temp = temp.next
	if int(val1) > int(val2):
		ans = int(val1) - int(val2)
	else:
		ans = int(val2) - int(val1)
	ans = str(ans)
	head = Node(ans[0])
	temp = head
	for i in range(1, len(ans)):
		node = Node(ans[i])
		temp.next = node
		temp = node
	return head


def subLinkedList(l1, l2):
	n1 = 0
	tmp = l1
	while tmp:
		n1 *= 10
		n1 += tmp.data
		tmp = tmp.next
	n2 = 0
	tmp = l2
	while tmp:
		n2 *= 10
		n2 += tmp.data
		tmp = tmp.next
	if n1 > n2:
		ans = n1 - n2
	else:
		ans = n2 - n1
	ans = str(ans)
	tmp = Node(ans[0])
	org = tmp
	for i in range(1, len(ans)):
		node = Node(ans[i])
		tmp.next = node
		tmp = tmp.next
	return org


def subLinkedList(l1, l2):
	ptr = l1
	num1 = 0
	while ptr:
		num1 = num1 * 10 + ptr.data
		ptr = ptr.next
	num2 = 0
	ptr = l2
	while ptr:
		num2 = num2 * 10 + ptr.data
		ptr = ptr.next
	diff = abs(num1 - num2)
	diff = str(diff)
	dum = Node(-1)
	ptr = dum
	for ele in diff:
		nn = Node(int(ele))
		ptr.next = nn
		ptr = ptr.next
	return dum.next


def subLinkedList(l1, l2):
	a1 = ''
	a2 = ''
	head1 = l1
	head2 = l2
	while l1:
		a1 += str(l1.data)
		l1 = l1.next
	while l2:
		a2 += str(l2.data)
		l2 = l2.next
	ans = list(str(abs(int(a1) - int(a2))))
	if int(a1) > int(a2):
		a = head1
	else:
		a = head2
	i = 0
	head = a
	while i < len(ans):
		a.data = int(ans[i])
		if i != len(ans) - 1:
			a = a.next
		i += 1
	a.next = None
	return head


def subLinkedList(l1, l2):
	temp1 = l1
	temp2 = l2
	num1 = 0
	num2 = 0
	len1 = 0
	len2 = 0
	while temp1 is not None:
		num1 = num1 * 10 + temp1.data
		temp1 = temp1.next
		len1 += 1
	while temp2 is not None:
		num2 = num2 * 10 + temp2.data
		temp2 = temp2.next
		len2 += 1
	res = num1 - num2
	temp1 = l1
	if len2 > len1:
		temp1 = l2
	temp2 = temp1
	temp3 = temp1
	res = str(abs(res))
	res = [*res]
	for i in res:
		temp1.data = i
		temp3 = temp1
		temp1 = temp1.next
	temp3.next = None
	return temp2


def reverseList(head):
	temp = head
	tmp = temp.next
	while temp.next is not None:
		temp.next = tmp.next
		tmp.next = head
		head = tmp
		tmp = temp.next
	return head

def subLinkedList(l1, l2):
	while l1.data == 0 and l1.next is not None:
		l1 = l1.next
	while l2.data == 0 and l2.next is not None:
		l2 = l2.next
	t1 = l1
	t2 = l2
	len1 = 0
	len2 = 0
	big = 0
	while t1.next or t2.next:
		if big == 0:
			if t1.data > t2.data:
				big = 1
			elif t1.data < t2.data:
				big = 2
		if t1.next:
			len1 += 1
			t1 = t1.next
		if t2.next:
			len2 += 1
			t2 = t2.next
	if len1 == len2:
		if big == 1:
			t1 = l1
			t2 = l2
		elif big == 2:
			t1 = l2
			t2 = l1
	elif len1 > len2:
		t1 = l1
		t2 = l2
	else:
		t1 = l2
		t2 = l1
	t1 = reverseList(t1)
	t2 = reverseList(t2)
	sub = LinkedList()
	borrow = False
	while t1 or t2:
		val1 = 0
		val2 = 0
		if t1 is not None:
			val1 = t1.data
			t1 = t1.next
		if t2 is not None:
			val2 = t2.data
			t2 = t2.next
		if borrow:
			val1 -= 1
			borrow = False
		if val1 < val2:
			val1 += 10
			borrow = True
		sub.insert(val1 - val2)
	sub = reverseList(sub.head)
	while sub.data == 0 and sub.next is not None:
		sub = sub.next
	return sub


def num(lst):
	num = ''
	curr_node = lst
	while curr_node:
		num += str(curr_node.data)
		curr_node = curr_node.next
	return int(num)

def subLinkedList(l1, l2):
	sub_num = num(l1) - num(l2)
	new_list = LinkedList()
	for x in str(abs(sub_num)):
		new_list.insert(x)
	return new_list.head


def subLinkedList(l1, l2):
	s = ' '
	k = ' '
	while l1:
		s += str(l1.data)
		l1 = l1.next
	while l2:
		k += str(l2.data)
		l2 = l2.next
	n1 = int(s)
	n2 = int(k)
	n = abs(n1 - n2)
	l = str(n)
	for i in l:
		print(i, end=' ')


def lengthOfLL(head):
	l = 0
	while head:
		head = head.next
		l += 1
	return l

def printLL(head):
	while head:
		print(head.data, '->', end=' ')
		head = head.next
	print()

def reverseLL(head):
	(prev, curr) = (None, head)
	while curr:
		next = curr.next
		curr.next = prev
		prev = curr
		curr = next
	return prev

def remove0s(head):
	while head:
		if head.data:
			return head
		head = head.next

def findLargest(head1, head2):
	while head1:
		if head1.data > head2.data:
			return True
		elif head1.data < head2.data:
			return False
		head1 = head1.next
		head2 = head2.next
	return True

def subLinkedList(l1, l2):
	l1 = remove0s(l1)
	l2 = remove0s(l2)
	len1 = lengthOfLL(l1)
	len2 = lengthOfLL(l2)
	(rev_l1, rev_l2, prev) = (None, None, None)
	if len1 > len2:
		rev_l1 = reverseLL(l1)
		rev_l2 = reverseLL(l2)
	elif len1 == len2:
		if findLargest(l1, l2):
			rev_l1 = reverseLL(l1)
			rev_l2 = reverseLL(l2)
		else:
			rev_l1 = reverseLL(l2)
			rev_l2 = reverseLL(l1)
	else:
		rev_l1 = reverseLL(l2)
		rev_l2 = reverseLL(l1)
	res = rev_l1
	carry = 0
	while rev_l2:
		if rev_l1.data < rev_l2.data + carry:
			rev_l1.data = rev_l1.data + 10 - rev_l2.data - carry
			carry = 1
		else:
			rev_l1.data = rev_l1.data - rev_l2.data - carry
			carry = 0
		rev_l2 = rev_l2.next
		prev = rev_l1
		rev_l1 = rev_l1.next
	while rev_l1:
		if rev_l1.data < carry:
			rev_l1.data = rev_l1.data + 10 - carry
			carry = 1
		else:
			rev_l1.data = rev_l1.data - carry
			carry = 0
		if rev_l1.next is None and rev_l1.data == 0:
			prev.next = None
			break
		prev = rev_l1
		rev_l1 = rev_l1.next
	res = remove0s(reverseLL(res))
	if res is None:
		return Node(0)
	return res


def make(head):
	c = 0
	curr = head
	while curr:
		c = c * 10 + curr.data
		curr = curr.next
	return c

def subLinkedList(l1, l2):
	a = make(l1)
	b = make(l2)
	c1 = abs(b - a)
	if c1 == 0:
		return Node(0)
	else:
		c1 = str(c1)
		dum = Node(0)
		ans = dum
		for i in c1:
			ans.next = Node(int(i))
			ans = ans.next
		return dum.next


def subLinkedList(l1, l2):
	int1 = ''
	current = l1
	while current:
		int1 += str(current.data)
		current = current.next
	int2 = ''
	current = l2
	while current:
		int2 += str(current.data)
		current = current.next
	new = abs(int(int1) - int(int2))
	head = None
	tail = None
	for i in str(new):
		if head != None:
			current.next = Node(i)
			current = current.next
		else:
			head = Node(i)
			tail = Node(i)
			current = head
	return head


def subLinkedList(l1, l2):
	s = ''
	k = ''
	while l1 != None:
		s = s + str(l1.data)
		l1 = l1.next
	while l2 != None:
		k = k + str(l2.data)
		l2 = l2.next
	s = int(s)
	k = int(k)
	if s > k:
		c = s - k
	else:
		c = k - s
	c = str(c)
	head = Node(int(c[0]))
	y = head
	for i in range(1, len(c)):
		z = Node(int(c[i]))
		y.next = z
		y = z
	return head


def subLinkedList(l1, l2):
	d1 = ''
	d2 = ''
	itr1 = l1
	while itr1:
		d1 += str(itr1.data)
		itr1 = itr1.next
	itr2 = l2
	while itr2:
		d2 += str(itr2.data)
		itr2 = itr2.next
	ans = str(abs(int(d1) - int(d2)))
	res = Node(0)
	cur = res
	for i in ans:
		cur.next = Node(int(i))
		cur = cur.next
	return res.next


def subLinkedList(l1, l2):
	n1 = ''
	n2 = ''
	while l1 is not None:
		n1 += str(l1.data)
		l1 = l1.next
	while l2 is not None:
		n2 += str(l2.data)
		l2 = l2.next
	n1 = int(n1)
	n2 = int(n2)
	n3 = abs(n1 - n2)
	n3 = str(n3)
	l1 = LinkedList()
	for i in n3:
		l1.insert(i)
	return l1.head


def solve(head):
	x = 0
	while head:
		x = x * 10 + head.data
		head = head.next
	return x

def subLinkedList(l1, l2):
	a = solve(l1)
	b = solve(l2)
	c = str(abs(a - b))
	head = Node(int(c[0]))
	temp = head
	for i in c[1:]:
		temp.next = Node(int(i))
		temp = temp.next
	return head


def subLinkedList(l1, l2):
	n1 = ''
	n2 = ''
	head = None
	while l1:
		n1 = n1 + str(l1.data)
		l1 = l1.next
	while l2:
		n2 = n2 + str(l2.data)
		l2 = l2.next
	if int(n1) > int(n2):
		ans = str(int(n1) - int(n2))
	else:
		ans = str(int(n2) - int(n1))
	for i in ans:
		n = Node(int(i))
		if head == None:
			head = n
			itr = n
		else:
			head.next = n
			head = head.next
	return itr


def subLinkedList(l1, l2):
	temp1 = l1
	s1 = ''
	c1 = 0
	while temp1:
		c1 += 1
		s1 += str(temp1.data)
		temp1 = temp1.next
	temp2 = l2
	s2 = ''
	c2 = 0
	while temp2:
		c2 += 1
		s2 += str(temp2.data)
		temp2 = temp2.next
	n = abs(int(s1) - int(s2))
	n = str(n)
	if c1 > c2:
		temp = l1
	else:
		temp = l2
	i = 0
	while temp:
		temp.data = int(n[i])
		if i == len(n) - 1:
			temp.next = None
		i = i + 1
		temp = temp.next
	if c1 > c2:
		temp = l1
	else:
		temp = l2
	return temp


def subLinkedList(l1, l2):
	s1 = ''
	s2 = ''
	while l1:
		s1 += str(l1.data)
		l1 = l1.next
	while l2:
		s2 += str(l2.data)
		l2 = l2.next
	val = ''
	if int(s1) >= int(s2):
		val = str(int(s1) - int(s2))
	else:
		val = str(int(s2) - int(s1))
	a = Node(0)
	tmp = a
	for i in val:
		i = int(i)
		tmp.next = Node(i)
		tmp = tmp.next
	return a.next


def subLinkedList(l1, l2):
	a = ''
	b = ''
	cur = l1
	while cur:
		a += str(cur.data)
		cur = cur.next
	cur1 = l2
	while cur1:
		b += str(cur1.data)
		cur1 = cur1.next
	n1 = int(a)
	n2 = int(b)
	n3 = str(abs(n1 - n2))
	x = Node(n3[0])
	t = x
	for (i, v) in enumerate(n3):
		if i != 0:
			new = Node(v)
			t.next = new
			t = new
	return x


def subLinkedList(l1, l2):
	p = q = 0
	while l1:
		p = p * 10 + l1.data
		l1 = l1.next
	while l2:
		q = q * 10 + l2.data
		l2 = l2.next
	p = abs(p - q)
	r = str(p)
	for i in range(0, len(r)):
		print(r[i], end=' ')


def subLinkedList(l1, l2):
	m = 0
	n = 0
	t1 = l1
	while t1:
		m = m * 10 + t1.data
		t1 = t1.next
	t1 = l2
	while t1:
		n = n * 10 + t1.data
		t1 = t1.next
	x = abs(n - m)
	s = str(x)
	new = Node(0)
	tmp = new
	for i in s:
		tmp.next = Node(int(i))
		tmp = tmp.next
	return new.next


def subLinkedList(l1, l2):
	curr1 = l1
	curr2 = l2
	s1 = ''
	s2 = ''
	while curr1:
		s1 += str(curr1.data)
		curr1 = curr1.next
	while curr2:
		s2 += str(curr2.data)
		curr2 = curr2.next
	s1 = int(s1)
	s2 = int(s2)
	x1 = max(s1, s2) - min(s1, s2)
	new = Node(0)
	tmp = new
	for i in str(x1):
		tmp.next = Node(int(i))
		tmp = tmp.next
	return new.next


def subLinkedList(l1, l2):
	s = ''
	while l1 != None:
		s = s + str(l1.data)
		l1 = l1.next
	r = ''
	while l2 != None:
		r = r + str(l2.data)
		l2 = l2.next
	a = str(abs(int(s) - int(r)))
	x = Node(a[0])
	x.next = None
	head = x
	for i in range(1, len(a)):
		y = Node(a[i])
		y.next = x.next
		x.next = y
		x = y
	return head


def subLinkedList(l1, l2):
	s1 = ''
	s2 = ''
	temp1 = l1
	temp2 = l2
	while temp1 != None:
		s1 += str(temp1.data)
		temp1 = temp1.next
	while temp2 != None:
		s2 += str(temp2.data)
		temp2 = temp2.next
	s3 = abs(int(s1) - int(s2))
	ans = list(str(s3))
	temp = Node(0)
	curr = temp
	for i in range(len(ans)):
		temp.next = Node(ans[i])
		temp = temp.next
	curr = curr.next
	return curr


def subLinkedList(l1, l2):
	(str1, str2) = ('', '')
	res = 0
	(temp1, temp2) = (l1, l2)
	while temp1:
		str1 += str(temp1.data)
		temp1 = temp1.next
	while temp2:
		str2 += str(temp2.data)
		temp2 = temp2.next
	(p, q) = (int(str1), int(str2))
	res = abs(p - q)
	m = str(res)
	ll = LinkedList()
	for i in m:
		ll.insert(i)
	return ll.head


def subLinkedList(l1, l2):
	(B, S) = BigSmall(l1, l2)
	B = reverseList(B)
	S = reverseList(S)
	N = None
	head = None
	borrow = 0
	while B and S:
		if B.data - borrow >= S.data:
			X = Node(B.data - borrow - S.data)
			borrow = 0
		elif B.data == 0:
			b = int('1' + str(B.data))
			X = Node(b - borrow - S.data)
			borrow = 1
		else:
			b = int('1' + str(B.data - borrow))
			X = Node(b - S.data)
			borrow = 1
		if not head:
			head = X
			N = X
		else:
			N.next = X
			N = X
		B = B.next
		S = S.next
	while B:
		if borrow == 1:
			val = B.data - borrow
			if B.data == 0:
				b = int('1' + str(B.data))
				X = Node(b - borrow)
				borrow = 1
				N.next = X
				N = X
			elif val == 0 and B.next:
				X = Node(val)
				borrow = 0
				N.next = X
				N = X
			elif val > 0:
				X = Node(val)
				borrow = 0
				N.next = X
				N = X
		else:
			X = Node(B.data)
			borrow = 0
			N.next = X
			N = X
		B = B.next
	head = reverseList(head)
	while head and head.next and (head.data == 0):
		head = head.next
	return head

def reverseList(head):
	pre = None
	trav = head
	nhead = head
	while trav:
		ele = trav.next
		trav.next = pre
		nhead = trav
		pre = trav
		trav = ele
	return nhead

def BigSmall(l1, l2):
	h = l1
	h1 = l2
	s = ''
	s1 = ''
	while h:
		s += str(h.data)
		h = h.next
	while h1:
		s1 += str(h1.data)
		h1 = h1.next
	if int(s1) == int(s):
		return (l1, l2)
	(_, X) = max((int(s), l1), (int(s1), l2))
	(_, Y) = min((int(s), l1), (int(s1), l2))
	return (X, Y)


def subLinkedList(l1, l2):
	a = []
	b = []
	while l1 is not None:
		a.append(str(l1.data))
		l1 = l1.next
	while l2 is not None:
		b.append(str(l2.data))
		l2 = l2.next
	a1 = ' '.join(a)
	b1 = ' '.join(b)
	s1 = ''
	s2 = ''
	for ele in a1:
		if ele != ' ':
			s1 = s1 + ele
	for ele in b1:
		if ele != ' ':
			s2 = s2 + ele
	a = str(abs(int(s1) - int(s2)))
	x = []
	for ele in a:
		x.append(int(ele))
	head = None
	tail = None
	x.append('z')
	for i in x:
		temp = Node(i)
		if i != 'z':
			if head is None:
				head = temp
				tail = temp
			else:
				tail.next = temp
				tail = temp
	return head


def subLinkedList(l1, l2):
	s1 = ''
	while l1:
		s1 += str(l1.data)
		l1 = l1.next
	s2 = ''
	while l2:
		s2 += str(l2.data)
		l2 = l2.next
	s1 = int(s1)
	s2 = int(s2)
	d = max(s1, s2) - min(s1, s2)
	s = list(map(int, str(d)))
	r = Node(0)
	l = r
	for i in s:
		l.next = Node(i)
		l = l.next
	return r.next


def subLinkedList(l1, l2):
	first = l1
	second = l2
	first_stack = []
	second_stack = []
	while first:
		first_stack.append(str(first.data))
		first = first.next
	while second:
		second_stack.append(str(second.data))
		second = second.next
	val = str(abs(int(''.join(first_stack)) - int(''.join(second_stack))))
	node = head = Node(int(val[0]))
	for i in range(1, len(val)):
		node.next = Node(int(val[i]))
		node = node.next
	return head


def subLinkedList(l1, l2):
	num1 = ''
	num2 = ''
	ptr = l1
	while ptr:
		num1 += str(ptr.data)
		ptr = ptr.next
	ptr = l2
	while ptr:
		num2 += str(ptr.data)
		ptr = ptr.next
	result = str(int(num1) - int(num2))
	ll = LinkedList()
	for i in result:
		if i == '-':
			continue
		ll.insert(i)
	return ll.head


class N:

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

def subLinkedList(l1, l2):
	l = ''
	m = ''
	while l1 is not None:
		l = l + str(l1.data)
		l1 = l1.next
	while l2 is not None:
		m = m + str(l2.data)
		l2 = l2.next
	c = str(abs(int(l) - int(m)))
	k = N(int(c[0]))
	q = k
	for i in range(1, len(c), 1):
		o = N(int(c[i]))
		q.next = o
		q = o
	return k


def subLinkedList(l1, l2):
	if not l1:
		return l2
	if not l2:
		return l1
	num1 = ''
	temp1 = l1
	while temp1:
		num1 += str(temp1.data)
		temp1 = temp1.next
	num2 = ''
	temp2 = l2
	while temp2:
		num2 += str(temp2.data)
		temp2 = temp2.next
	res = str(abs(int(num1) - int(num2)))
	l = LinkedList()
	for r in res:
		l.insert(r)
	return l.head


def reverseList(head):
	pre = None
	while head:
		temp = head.next
		head.next = pre
		pre = head
		head = temp
	return pre

def listLen(head):
	ans = 0
	while head:
		ans += 1
		head = head.next
	return ans

def isLT_sameLength(l1, l2):
	while l1 and l2:
		if l1.data != l2.data:
			if l1.data < l2.data:
				return True
			else:
				return False
		l1 = l1.next
		l2 = l2.next
	return False

def getVal(node):
	return node.data if node else 0

def removeInitialZeros(head):
	while head.data == 0 and head.next:
		head = head.next
	return head

def subLinkedList(l1, l2):
	l1 = removeInitialZeros(l1)
	l2 = removeInitialZeros(l2)
	n1 = listLen(l1)
	n2 = listLen(l2)
	if n1 < n2 or (n1 == n2 and isLT_sameLength(l1, l2)):
		(l1, l2) = (l2, l1)
		(n1, n2) = (n2, n1)
	l1cur = l1 = reverseList(l1)
	l2cur = l2 = reverseList(l2)
	ans = anscur = Node(None)
	reduceNext = False
	while l1cur or l2cur:
		val_l1 = getVal(l1cur)
		val_l2 = getVal(l2cur)
		if l1cur:
			l1cur = l1cur.next
		if l2cur:
			l2cur = l2cur.next
		if reduceNext == True:
			val_l1 -= 1
			reduceNext = False
		if val_l1 < val_l2:
			val_l1 += 10
			reduceNext = True
		newnode = Node(val_l1 - val_l2)
		anscur.next = newnode
		anscur = anscur.next
	ans = ans.next
	ans = reverseList(ans)
	return removeInitialZeros(ans)


def subLinkedList(l1, l2):
	(val1, val2) = (0, 0)
	while l1 or l2:
		val1 = val1 * 10 + l1.data if l1 else val1
		val2 = val2 * 10 + l2.data if l2 else val2
		l1 = l1.next if l1 else None
		l2 = l2.next if l2 else None
	val1 = abs(val1 - val2)
	dummy = Node(0)
	new = dummy
	a = str(val1)
	for i in range(len(a)):
		new.next = Node(int(a[i]))
		new = new.next
	return dummy.next


def subLinkedList(l1, l2):
	c = 0
	while l1:
		c = c * 10 + l1.data
		l1 = l1.next
	p = 0
	while l2:
		p = p * 10 + l2.data
		l2 = l2.next
	c = abs(c - p)
	op = list(str(c))
	lp = Node(0)
	kk = lp
	for i in op:
		kk.next = Node(i)
		kk = kk.next
	return lp.next


def subLinkedList(l1, l2):
	lst1 = []
	lst2 = []
	while l1:
		lst1.append(str(l1.data))
		l1 = l1.next
	while l2:
		lst2.append(str(l2.data))
		l2 = l2.next
	n1 = ''.join(lst1)
	n2 = ''.join(lst2)
	c = str(abs(int(n1) - int(n2)))
	k = []
	for ele in c:
		k.append(ele)
	head = None
	cur = None
	for i in range(len(k)):
		newnode = Node(k[i])
		if not head:
			head = newnode
			cur = newnode
		else:
			cur.next = newnode
			cur = newnode
	return head


def subLinkedList(l1, l2):

	def printl(temp):
		while temp:
			print(temp.data, end=' ')
			temp = temp.next
		print()

	def rev(temp):
		prev = None
		while temp:
			next = temp.next
			temp.next = prev
			prev = temp
			temp = next
		return prev

	def removezeros(ans):
		while ans and ans.data == 0:
			ans = ans.next
		return ans
	l1 = removezeros(l1)
	l2 = removezeros(l2)
	if not l1:
		return l2
	if not l2:
		return l1
	ans = Node(0)
	dummy = ans
	n1 = 0
	n2 = 0
	temp = l1
	while temp:
		n1 += 1
		temp = temp.next
	temp = l2
	isSame = False
	while temp:
		n2 += 1
		temp = temp.next
	if n1 > n2:
		small = l2
		large = l1
	elif n2 > n1:
		small = l1
		large = l2
	else:
		temp1 = l1
		temp2 = l2
		isSame = True
		while temp1:
			if temp1.data > temp2.data:
				large = l1
				small = l2
				isSame = False
				break
			elif temp2.data > temp1.data:
				large = l2
				small = l1
				isSame = False
				break
			else:
				temp1 = temp1.next
				temp2 = temp2.next
	if isSame:
		return Node(0)
	small = rev(small)
	large = rev(large)
	temp1 = large
	temp2 = small
	borrow = 0
	while temp1:
		a = temp1.data
		b = 0
		if temp2:
			b = temp2.data
		d = a - b
		if d < 0:
			d = d + 10
			nextborrow = 1
		else:
			nextborrow = 0
		d -= borrow
		if d < 0:
			d += 10
			nextborrow = 1
		dummy.next = Node(d)
		dummy = dummy.next
		temp1 = temp1.next
		if temp2:
			temp2 = temp2.next
		borrow = nextborrow
	ans = ans.next
	ans = rev(ans)
	ans = removezeros(ans)
	return ans


def subLinkedList(l1, l2):
	a = ''
	while l1:
		a += str(l1.data)
		l1 = l1.next
	b = ''
	while l2:
		b += str(l2.data)
		l2 = l2.next
	n = Node(0)
	m = n
	for i in str(abs(int(a) - int(b))):
		n.next = Node(i)
		n = n.next
	return m.next


def subLinkedList(l1, l2):
	li = []
	tmp = Node(None)
	tmp = l1
	while tmp != None:
		li.append(tmp.data)
		tmp = tmp.next
	string1 = ''.join(map(str, li))
	li.clear()
	tmp = l2
	while tmp != None:
		li.append(tmp.data)
		tmp = tmp.next
	string2 = ''.join(map(str, li))
	tmp = int(string1) - int(string2)
	tmp = ' '.join(str(abs(tmp)))
	tmp = tmp.split()
	headd = LinkedList()
	for i in tmp:
		headd.insert(i)
	return headd.head


def subLinkedList(l1, l2):
	if not l1 or not l2:
		return
	p1 = l1
	p2 = l2
	str1 = str(l1.data)
	str2 = str(l2.data)
	while p1.next:
		p1 = p1.next
		str1 += str(p1.data)
	while p2.next:
		p2 = p2.next
		str2 += str(p2.data)
	val = str(abs(int(str1) - int(str2)))
	myLL = Node(int(val[0]))
	p1 = myLL
	for i in range(1, len(val)):
		p1.next = Node(int(val[i]))
		p1 = p1.next
	return myLL


def subLinkedList(l1, l2):
	s1 = ''
	s2 = ''
	curr1 = l1
	curr2 = l2
	while curr1 != None:
		s1 += str(curr1.data)
		curr1 = curr1.next
	while curr2 != None:
		s2 += str(curr2.data)
		curr2 = curr2.next
	res = str(abs(int(s1) - int(s2)))
	newH = Node(res[0])
	newC = newH
	for i in range(1, len(res)):
		newC.next = Node(res[i])
		newC = newC.next
	return newH


def subLinkedList(l1, l2):
	val1 = ''
	val2 = ''
	iter1 = l1
	while iter1:
		val1 += str(iter1.data)
		iter1 = iter1.next
	iter2 = l2
	while iter2:
		val2 += str(iter2.data)
		iter2 = iter2.next
	answer = str(abs(int(val1) - int(val2)))
	res = Node(0)
	current = res
	for d in answer:
		current.next = Node(d)
		current = current.next
	return res.next


def subLinkedList(l1, l2):
	h = l1
	h1 = l2
	x = ''
	y = ''
	while h != None:
		x += str(h.data)
		h = h.next
	while h1 != None:
		y += str(h1.data)
		h1 = h1.next
	p = ''
	if x > y:
		p = str(abs(int(x) - int(y)))
	else:
		p = str(abs(int(y) - int(x)))
	tmp = l1
	n = len(p)
	for i in range(n):
		if i != n - 1 and tmp.next == None:
			tmp.next = l2
		tmp.data = p[i]
		if i == n - 1 and tmp != None:
			tmp.next = None
		tmp = tmp.next
	return l1


def subLinkedList(l1, l2):

	def reverse(head):
		if head == None:
			return head
		prev = None
		curr = head
		while curr != None:
			nex = curr.next
			curr.next = prev
			prev = curr
			curr = nex
		return prev
	num1 = 0
	num2 = 0
	temp = l1
	while temp != None:
		num1 = num1 * 10 + temp.data
		temp = temp.next
	temp = l2
	while temp != None:
		num2 = num2 * 10 + temp.data
		temp = temp.next
	ans = abs(num1 - num2)
	if ans == 0:
		return Node(0)
	anshead = Node(0)
	anstr = str(ans)
	temp = anshead
	for i in anstr:
		temp.next = Node(int(i))
		temp = temp.next
	return anshead.next


def subLinkedList(l1, l2):
	s1 = ''
	s2 = ''
	while l1:
		s1 += str(l1.data)
		l1 = l1.next
	while l2:
		s2 += str(l2.data)
		l2 = l2.next
	s1 = int(s1)
	s2 = int(s2)
	if s1 > s2:
		k = s1 - s2
	else:
		k = s2 - s1
	f = True
	for i in str(k):
		if f:
			head = Node(i)
			temp = head
			f = False
		else:
			temp.next = Node(i)
			temp = temp.next
	return head


def subLinkedList(l1, l2):

	def change(head):
		c = 0
		curr = head
		while curr:
			c = c * 10 + curr.data
			curr = curr.next
		return c
	a = change(l1)
	b = change(l2)
	ans = abs(a - b)
	if ans == 0:
		return Node(0)
	ans = str(ans)
	dummy = Node(0)
	res = dummy
	for i in ans:
		res.next = Node(int(i))
		res = res.next
	return dummy.next


def subLinkedList(l1, l2):
	(c, d) = (0, 0)
	while l1:
		c = c * 10 + l1.data
		l1 = l1.next
	while l2:
		d = d * 10 + l2.data
		l2 = l2.next
	c = abs(c - d)
	temp = Node(0)
	ans = temp
	for i in str(c):
		temp.next = Node(int(i))
		temp = temp.next
	return ans.next


def subLinkedList(first, second):
	(c, b) = (0, 0)
	while first:
		c = c * 10 + first.data
		first = first.next
	while second:
		b = b * 10 + second.data
		second = second.next
	a = Node(0)
	temp = a
	for i in str(abs(c - b)):
		temp.next = Node(i)
		temp = temp.next
	return a.next


def subLinkedList(l1, l2):
	h = ''
	l = ''
	while l1:
		h = h + str(l1.data)
		l1 = l1.next
	while l2:
		l = l + str(l2.data)
		l2 = l2.next
	res = list(tuple(str(abs(int(h) - int(l)))))
	obj = LinkedList()
	for i in res:
		obj.insert(i)
	return obj.head