I have a method which will merger two sorted list together. The function works, but I saw some duplicated code.
Any ideas to make the code more elegant?
class Node(object):
def __init__(self, data=None, next_node=None):
self.data = data
self.next = next_node
def MergeLists(headA, headB):
head = tail = Node('',None)
while headA or headB:
# duplicated code here
if headA is not None:
if (headB and headA.data<=headB.data) or (headB is None):
tail.next = headA
tail = headA
headA = headA.next
if headB is not None:
if (headA and headB.data <= headA.data) or (headA is None):
tail.next = headB
tail = headB
headB = headB.next
return head.next