Click Here For Problem
Approach:
Merging Linked Lists:
First we traverse up to end of first linked list and assign next pointer of last node to head of second linked list
Sorting linked List:
1.append each element to a stack
2.sort them using sorted function
3.Now pop each element from stack one by one and start
inserting poped element in to linked list from head
Python Code:
def mergeResult(h1,h2):
cur = h1
while cur.next:
cur = cur.next
cur.next = h2
lst = []
cur = h1
while cur:
lst.append(cur.data)
cur = cur.next
lst = sorted(lst)
cur = h1
while cur and len(lst)>0:
cur.data = lst.pop()
cur = cur.next
return h1
0 Comments