public void merge_sorted_list(LinkedList other) throws Exception {
// write your code here
Node ptr1 = this.head;
Node ptr2 = other.head;
Node newHead = null;
Node temp = ptr1 == null ? ptr2 : ptr1;
while(ptr1 != null && ptr2 != null) {
if (ptr1.data < ptr2.data) {
temp = ptr1;
ptr1 = ptr1.next;
temp.next = ptr2;
} else {
temp = ptr2;
ptr2 = ptr2.next;
temp.next = ptr1;
}
if (newHead == null) {
newHead = temp;
}
}
while (ptr1 != null) {
temp.next = ptr1;
ptr1 = ptr1.next;
temp = ptr1;
}
while (ptr2 != null) {
temp.next = ptr2;
ptr2 = ptr2.next;
temp = ptr2;
}
head = newHead;
}