Merge two sorted linked lists (java)

Merge two sorted linked lists

Input two monotonically increasing linked lists and output the combined linked list of the two linked lists. Of course, we need the combined linked list to satisfy the monotonically non-decreasing rule

Idea 1: Create a new linked list, traverse the two linked lists from front to back, and whoever is younger will advance the new linked list. in a recursive way.

Idea 2: Idea 1 adopts a recursive method, and Idea 2 adopts a non-recursive method .

idea one

Create a new linked list, traverse the two linked lists from front to back, whoever is younger is the first to create a new linked list. in a recursive way.

	
public static ListNode listMerge(ListNode list1, ListNode list2){
	if (list1 == null) return list2;//Judging that a linked list is empty, return another linked list.
	if (list2 == null) return list1;
 	ListNode mergeList = null;//Define a linked list as the return value
 	if (list1.val <= list2.val) {//If the value of list1 is relatively small, first assign list1 to mergeList
 		mergeList = list1;
 		mergeList.next = listMerge(list1.next, list2);//Do recursion, find the value of the next node in the linked list
 	}
 	else {//If the value of list2 is relatively small, first assign list1 to mergeList
 		mergeList = list2;
 		mergeList.next = listMerge(list1, list2.next);//Do recursion, find the value of the next node in the linked list
 	}
 return mergeList;		
	
}
		

It can be simplified by using the ternary operator

mergeList = list1.val > list2.val ? list2:list1;
mergeList.next = list1.val > list2.val ? listMerge(list1, list2.next):listMerge(list1.next, list2);

idea two

Idea 1 adopts a recursive method, and Idea 2 adopts a non-recursive method.

public static ListNode listMerge2(ListNode list1, ListNode list2){
	if (list1 == null) return list2;//Judging that a linked list is empty, return another linked list.
	if (list2 == null) return list1;
		
		
	ListNode mergeList = new ListNode(0);//Why not null. Because there is mergeList.next below
//	ListNode mergeList = null;
	ListNode temp1 = list1;
	ListNode temp2 = list2;
	ListNode head = mergeList;//Keep its pointer position
	while(temp1 != null && temp2 != null){
		if (temp1.val <= temp2.val) {
			mergeList.next = temp1;
			mergeList = mergeList.next;
			temp1 = temp1.next;
		}
		else {//If the value of list2 is relatively small, first assign list1 to mergeList
			mergeList.next = temp2;
			mergeList = mergeList.next;
			temp2 = temp2.next;
		}
	}//while end. One of the linked lists has been traversed
	if (temp1 != null) {
		mergeList.next = temp1;
		mergeList = mergeList.next;
		temp1 = temp1.next;
	}
	if (temp2 != null) {
		mergeList.next = temp2;
		mergeList = mergeList.next;
		temp2 = temp2.next;
	}
	mergeList = head.next;
	return mergeList;
	}

There are two linked lists. Since the data structure of linked list is easy to insert, we can insert one linked list into the other linked list.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325162704&siteId=291194637