"""
Merge two linked lists
head could be None as well for empty list
Node is defined as
class Node(object):
def __init__(self, data=None, next_node=None):
self.data = data
self.next = next_node
return back the head of the linked list in the below method.
"""
def MergeLists(headA, headB):
if (headA==None) | (headB==None):
if(headA == None):
return headB
else:
return headA
if headA.data > headB.data: # headA points to the smaller one
temp = headA
headA = headB
headB = temp
head = headA
curA = headA.next # curA one step ahead
curB = headB
while (curA!=None) & (curB!=None):
if (curA.data>=curB.data):
headB =headB.next
curB.next = curA
headA.next = curB
headA = curA
curA = curA.next
curB = headB
else:
headA =curA
curA = curA.next
if curA == None:
headA.next = headB
return head
'Python 문제풀이 > 프로그래머스&해커랭크' 카테고리의 다른 글
Recursive Digit Sum (해커랭크)? (0) | 2022.04.26 |
---|---|
Grid Challenge (해커랭크) (0) | 2022.04.26 |
Caesar Cipher (해커랭크) (0) | 2022.04.25 |
Zig Zag Sequence (해커랭크) (0) | 2022.04.25 |
Counting Sort 1 (해커랭크) (0) | 2022.04.25 |