offer17--合并两个有序链表

# coding=utf-8
class Listcode:
    def __init__(self,x):
        self.val=x
        self.next=None
class Solution:
    def Merge(self,head1,head2):
        if head1==None:
            return head2
        elif head2==None:
            return head1
        mergehead=None
        if head1.val<head2.val:
            mergehead=head1
            mergehead.next=self.Merge(head1.next,head2)
        else:
            mergehead=head2
            mergehead.next=self.Merge(head1,head2.next)
        return mergehead
if __name__ == '__main__':
    node1=Listcode(1)
    node2=Listcode(3)
    node3=Listcode(5)
    node1.next=node2
    node2.next=node3
    node4=Listcode(2)
    node5=Listcode(4)
    node6=Listcode(6)
    node4.next=node5
    node5.next=node6
    s=Solution()
    newList = s.Merge(node1,node4)

    while newList is not None:
        print newList.val
        newList = newList.next
        pass



    print '=================='
    print node4.next.val
点赞