Title: Enter two ascending sorted lists, merge the two linked lists and make the nodes in the new list ascending.
Problem-solving ideas: Two linked lists are already ordered, and when traversing the linked list, simply compare the current position size of the two linked list and remove the smallest additions to the new linked list.
It can be solved in two ways, recursive and cyclic.
1 Packagesolution;2 3 4 Public classno17mergesortedlists {5 6 Public Static classListNode {7 intdata;8 ListNode Next;9 Ten PublicListNode () { One A } - - PublicListNode (intvalue, ListNode next) { the This. data =value; - This. Next =Next; - } - } + - Public Static voidprint (ListNode head) { + if(Head = =NULL) ASystem.out.println ("Current linked list is empty"); at while(Head! =NULL) { -System.out.print (Head.data + ","); -Head =Head.next; - } - System.out.println (); - } inRecursively merge two sorted lists - Public Staticlistnode Merge (ListNode head1, ListNode head2) { to if(Head1 = =NULL) + returnhead2; - if(Head2 = =NULL) the returnHead1; *ListNode Mergedhead =NULL; $ if(Head1.data <head2.data) {Panax NotoginsengMergedhead =Head1; -Mergedhead.next =merge (Head1.next, head2); the}Else { + //if the value of two nodes is the same, the second one is returned AMergedhead =head2; theMergedhead.next =merge (Head1, head2.next); + } - returnMergedhead; $ } $Compare the current nodes of the two linked lists in turn and add them to the new linked list - Public StaticListNode mergesortedlist (ListNode head1, ListNode head2) { - if(Head1 = =NULL) the returnhead2; - if(Head2 = =NULL) {Wuyi returnHead1; the } -ListNode Newhead =NULL; WuListNode NewNode =NULL; -ListNode List1 =Head1; AboutListNode List2 =head2; $ //find a new head node . - if(List1.data <list2.data) { -Newhead =List1; -List1 =List1.next; A}Else { +Newhead =List2; theList2 =List2.next; - } $NewNode =Newhead; the //Merging other nodes the while(List1! =NULL&& List2! =NULL) { the if(List1.data <list2.data) { theNewnode.next =List1; -List1 =List1.next; in}Else { theNewnode.next =List2; theList2 =List2.next; About } theNewNode =Newnode.next; the } the //One linked list is merged, then the remaining list is merged directly to the end of the new chain. + if(List1 = =NULL) { -Newnode.next =List2; the}Else {BayiNewnode.next =List1; the } the returnNewhead; - } - the Public Static voidMain (string[] args) { theListNode Node1 =NewListNode (7,NULL); theListNode Node2 =NewListNode (4, Node1); theListNode Node3 =NewListNode (3, Node2); -ListNode Head1 =NewListNode (1, node3); the theListNode NODE5 =NewListNode (8,NULL); theListNode Node6 =NewListNode (6, NODE5);94ListNode Node7 =NewListNode (4, node6); theListNode head2 =NewListNode (2, node7); the //The test contains the same list of two nodes that are worth merging, and Head1 and merged1 all point to the head node of the merged new list. the //ListNode merged1 = Merge (Head1, head2);98 //print (merged1); About // //tests the merge of a linked list with a null-headed pointer, since head2 points to the second node of the new linked list after the previous step, so the number of nodes in the output is the total number-1 - //Print (merge (head2, null));101 // //test only one node in two linked lists102 //Print (merge (null, new ListNode (max, NULL) ));103ListNode merged1 =mergesortedlist (Head1, head2);104 print (merged1); the //tests the merge of a linked list with a null-headed pointer, since head2 points to the second node of the new linked list after the previous step, so the number of nodes in the output is the total number-1106Print (Mergesortedlist (head2,NULL));107 //test only one node in two linked lists108Print (Mergesortedlist (NULL,NewListNode (10,NULL)));109 } the 111}
Sword Point offer question 17: Merge two sorted lists