Java implementation of two ordered single-linked list merging into an ordered single-linked list

Source: Internet
Author: User
Tags comparable

As a comment, it is easy to review it yourself.

Import Java.util.Arrays;PublicClassMergeSort {PublicStaticClasslinkednode<VExtendscomparable<v>> {public V value;Public linkednode<v> Next;PublicLinkednode (V value) {This.value = value; }PublicLinkednode (V value, linkednode<v> next) {This.value = value;This.next = Next; } }PublicStatic <t extends comparable<t>> linkednode<t>Preparesortedlinkedlist (t...list) {arrays.sort (list); Linkednode<t> head =New Linkednode<> (list[0]); Linkednode<t> begin = Head;for (int i =1; i < list.length; i++) {Begin.next =New Linkednode<> (List[i]); begin = Begin.next; }return head; }PublicStatic <t extends comparable<t>> linkednode<t>Clonelinkedlist (linkednode<t> head) {if (head = =NULL) {ReturnNull } linkednode<t> result =New Linkednode<> (Head.value); linkednode<t> clonedlist = result;while (True) {head = Head.next;if (head! =NULL) {Clonedlist.next =New Linkednode<> (Head.value); Clonedlist = Clonedlist.next; }else {Break } }return result; }PublicStatic <t extends Comparable<t>>voidDumplinkedlist (linkednode<t> head) {while (True) {if (head! =NULL) {System.out.print (string.valueof (head.value) +‘ ‘); }else {Break } head = Head.next; } System.out.println (); }PublicStaticvoidMain (string[] args) {linkednode<string> head11 = preparesortedlinkedlist ("Dump","Just","Found","Get"); linkednode<string> head21 = clonelinkedlist (HEAD11); Dumplinkedlist (HEAD11); System.out.println ("-------------------------------------------"); Dumplinkedlist (HEAD21); System.out.println ("-------------------------------------------"); linkednode<string> head12 = Preparesortedlinkedlist ("Get","Zara","Yes","But","Row","OK","another"); linkednode<string> head22 = clonelinkedlist (HEAD12); Dumplinkedlist (HEAD12); System.out.println ("-------------------------------------------"); Dumplinkedlist (HEAD22);End Prepare System.out.println ("\n++++++++++++++++++++++++++++++++++++++++++\n");Start Test linkednode<string> result1 = MergeSortedLinkedList1 (Head11, head12); Dumplinkedlist (RESULT1); System.out.println ("-------------------------------------------"); linkednode<string> result2 = MergeSortedLinkedList2 (Head21, head22); Dumplinkedlist (RESULT2); }/** recusive. Return head node as ascending. Would change parameters, non reentrant. */PublicStatic <t extends comparable<t>> linkednode<t>MergeSortedLinkedList1 (linkednode<t> A, linkednode<t> b) {linkednode<t> head;if (A = =NULL) {return b; }if (b = =NULL) {return A; }if (Isfirstlessthansecond (A.value, B.value)) {head = A; Head.next = MergeSortedLinkedList1 (A.next, b);}else {head = b; head.next = MergeSortedLinkedList1 (A, b.next);}return head; }/** virtual node + loop. Return head node as ascending. Would change parameters, non reentrant. */PublicStatic <t extends comparable<t>> linkednode<t>MergeSortedLinkedList2 (linkednode<t> A, linkednode<t> b) {linkednode<t> Header =NULL, head =Nullif (A = =NULL) {return b; }if (b = =NULL) {return A; }while (True) {if (Isfirstlessthansecond (A.value, B.value)) {if (head = =NULL) {Header = head = A;}else {head.next = A; head = Head.next;} a = A.next; }else {if (head = =NULL) {Header = head = b;}else {head.next = b; head = Head.next;} b = B.next; }if (A = =NULL) {head.next = b;Break }Elseif (b = =NULL) {head.next = A;Break ;} } return header;} /** * 1. NULL is smallest; * 2. If first = = NULL && second = = NULL then return true; * 3. If first equals seco nd then return false; *  /private static <t extends Comparable<t>> boolean isfirstlessthansecond (t first, T second) { if (first = = null) { return true;} if (second = = null) { return false;} return First.compareto (second) < 0;}}            

Java implementation of two ordered single-linked list merging into an ordered single-linked list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.