1
+ """
2
+ Problem Link: https://practice.geeksforgeeks.org/problems/merge-two-sorted-linked-lists/1
3
+
4
+ Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge
5
+ both of the list (in-place) and return head of the merged list.
6
+ Note: It is strongly recommended to do merging in-place using O(1) extra space.
7
+
8
+ Input:
9
+ First line of input contains number of testcases T. For each testcase, first line of
10
+ input contains N and M, and next two line contains N and M sorted elements in two lines for each.
11
+
12
+ Output:
13
+ For each testcase, print the merged list in sorted form.
14
+
15
+ User Task:
16
+ The task is to complete the function sortedMerge() which takes references to the heads of
17
+ two linked lists as the arguments and returns the head of merged linked list.
18
+
19
+ Constraints:
20
+ 1 <= T <= 200
21
+ 1 <= N, M <= 103
22
+ 1 <= Node's data <= 103
23
+
24
+ Example:
25
+ Input:
26
+ 2
27
+ 4 3
28
+ 5 10 15 40
29
+ 2 3 20
30
+ 2 2
31
+ 1 1
32
+ 2 4
33
+
34
+ Output:
35
+ 2 3 5 10 15 20 40
36
+ 1 1 2 4
37
+
38
+ Explanation:
39
+ Testcase 1: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40.
40
+ """
41
+ #Initial Template for Python 3
42
+ # Node Class
43
+ class Node :
44
+ def __init__ (self , data ): # data -> value stored in node
45
+ self .data = data
46
+ self .next = None
47
+ # Linked List Class
48
+ class LinkedList :
49
+ def __init__ (self ):
50
+ self .head = None
51
+ # creates a new node with given value and appends it at the end of the linked list
52
+ def append (self , new_value ):
53
+ new_node = Node (new_value )
54
+ if self .head is None :
55
+ self .head = new_node
56
+ return
57
+ curr_node = self .head
58
+ while curr_node .next is not None :
59
+ curr_node = curr_node .next
60
+ curr_node .next = new_node
61
+ # prints the elements of linked list starting with head
62
+ def printList (self ):
63
+ if self .head is None :
64
+ print (' ' )
65
+ return
66
+ curr_node = self .head
67
+ while curr_node :
68
+ print (curr_node .data ,end = " " )
69
+ curr_node = curr_node .next
70
+ print (' ' )
71
+ if __name__ == '__main__' :
72
+ t = int (input ())
73
+ for cases in range (t ):
74
+ n ,m = map (int , input ().strip ().split ())
75
+ a = LinkedList () # create a new linked list 'a'.
76
+ b = LinkedList () # create a new linked list 'b'.
77
+ nodes_a = list (map (int , input ().strip ().split ()))
78
+ nodes_b = list (map (int , input ().strip ().split ()))
79
+ for x in nodes_a :
80
+ a .append (x )
81
+ for x in nodes_b :
82
+ b .append (x )
83
+ a .head = merge (a .head ,b .head )
84
+ a .printList ()
85
+ ''' This is a function problem.You only need to complete the function given below '''
86
+ #User function Template for python3
87
+ '''
88
+ Function to merge two sorted lists in one
89
+ using constant space.
90
+
91
+ Function Arguments: head_a and head_b (head reference of both the sorted lists)
92
+ Return Type: head of the obtained list after merger.
93
+ {
94
+ # Node Class
95
+ class Node:
96
+ def __init__(self, data): # data -> value stored in node
97
+ self.data = data
98
+ self.next = None
99
+ }
100
+ '''
101
+ def merge (head_a ,head_b ):
102
+ #code here
103
+ if not head_a or not head_b :
104
+ return head_a or head_b
105
+ newHead = Node (0 )
106
+ prev = newHead
107
+ while head_a and head_b :
108
+ if head_a .data < head_b .data :
109
+ prev .next = head_a
110
+ prev = head_a
111
+ head_a = head_a .next
112
+ else :
113
+ prev .next = head_b
114
+ prev = head_b
115
+ head_b = head_b .next
116
+ prev .next = head_a or head_b
117
+ return newHead .next
0 commit comments