I am trying to merge two ordered singly linked list. I tried searching where the problem is but I am not able to find the answer. The output is not what I am expecting. Output is given below
class Test
{
Node head; // head of list
class Node
{
int data;
Node next;
Node(int d){
data = d;
next = null;
}
}
void sortedInsert(Node newNode)
{
Node current;
if (head == null || head.data >= newNode.data)
{
newNode.next = head;
head = newNode;
}
else {
current = head;
while (current.next != null && current.next.data < newNode.data)
current = current.next;
newNode.next = current.next;
current.next = newNode;
}
}
Node newNode(int data)
{
Node x = new Node(data);
return x;
}
/* Function to print linked list */
void printList()
{
Node temp = head;
while (temp != null)
{
System.out.print(temp.data+"-> ");
temp = temp.next;
}
System.out.print("null\n");
}
Node mergeLists(Node list1, Node list2) {
Node result;
if (list1 == null) return list2;
if (list2 == null) return list1;
if (list1.data < list2.data) {
result = list1;
result.next = mergeLists(list1.next, list2);
} else {
result = list2;
result.next = mergeLists(list2.next, list1);
}
return result;
}
/* Drier function to test above methods */
public static void main(String args[])
{
Test oneList = new Test();
Test twoList = new Test();
Test joinList = new Test();
Node l1,l2,join;
//First linked list
l1 = oneList.newNode(11);
oneList.sortedInsert(l1);
l1 = oneList.newNode(13);
oneList.sortedInsert(l1);
l1 = oneList.newNode(12);
oneList.sortedInsert(l1);
l1 = oneList.newNode(17);
oneList.sortedInsert(l1);
l1 = oneList.newNode(15);
oneList.sortedInsert(l1);
l1 = oneList.newNode(19);
oneList.sortedInsert(l1);
System.out.println("First List");
oneList.printList();
//Second Linked List
l2 = twoList.newNode(1);
twoList.sortedInsert(l2);
l2 = twoList.newNode(5);
twoList.sortedInsert(l2);
l2 = twoList.newNode(3);
twoList.sortedInsert(l2);
l2 = twoList.newNode(7);
twoList.sortedInsert(l2);
l2 = twoList.newNode(4);
twoList.sortedInsert(l2);
l2 = twoList.newNode(19);
twoList.sortedInsert(l2);
System.out.println("Created Second Linked List");
twoList.printList();
join=joinList.mergeLists(l1,l2);
System.out.println("Merge");
joinList.sortedInsert(join);
joinList.printList();
}
}
OUTPUT:
First List
11-> 12-> 13-> 15-> 17-> 19-> null
Created Second Linked List
1-> 3-> 4-> 5-> 7-> 19-> null
Merge
19-> null
One problem is in the main method
oneList.sortedInsert(l1);
l1 = oneList.newNode(15);
oneList.sortedInsert(l1);
l1 = oneList.newNode(19);
l1
is the list 19 -> NULL and the same for l2
19 -> NULL. So the merge seem correct, the problem is that you are overwriting l1
and l2
with the last element rather than the first. I hope this helps.
You do not want to do this recursively, because if the lists are even moderately large you will overflow the stack. Merging two lists is a simple iterative operation. The basic idea is:
if (list1 == null) return list2;
if (list2 == null) return list1;
Node head = null;
Node l1 = list1;
Node l2 = list2;
if (l1.data < l2.data)
{
head = l1;
l1 = l1.next;
}
else
{
head = l2;
l2 = l2.next;
}
Node current = head;
// while not at end of either list
while (l1 != null && l2 != null)
{
if (l1.data < l2.data)
{
current.next = l1;
l1 = l1.next;
}
else
{
current.next = l2;
l2 = l2.next;
}
current = current.next;
current.next = null;
}
// at this point, you are at the end of one or both of the lists
// Pick up the potential remainder from the list that's not at the end.
// Note that only one of these loops will be entered.
while (l1 != null)
{
current.next = l1;
current = current.next;
current.next = null;
l1 = l1.next;
}
while (l2 != null)
{
current.next = l2;
current = current.next;
current.next = null;
l2 = l2.next;
}
return head;
That's the simple verbose solution. You can reduce the amount of code a bit by creating a method that appends a node to the new list, but the logic is the same.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.