递归
package lhh.fight.LinkTest; public class SingleLinkMerge { static class ListNode { int data; ListNode next; public ListNode(){} public ListNode(int data) { this.data = data; } } public static void displayList(ListNode first) { System.out.print("List(first-->last):"); ListNode current = first; while (current != null) { System.out.print(current.data + " "); current = current.next; } } public static ListNode merageTwoLists(ListNode L1,ListNode L2) { if(L1 == null)return L2; if(L2 == null)return L1; if(L1.data < L2.data) { L1.next = merageTwoLists(L1.next,L2); return L1; }else { L2.next = merageTwoLists(L1,L2.next); return L2; } } public static void main(String[] args) { ListNode L1 = new ListNode(1); ListNode xnode1 = new ListNode(4); ListNode xnode2 = new ListNode(5); ListNode xnode3 = new ListNode(8); ListNode xnode4 = new ListNode(9); L1.next = xnode1; xnode1.next = xnode2; xnode2.next = xnode3; xnode3.next = xnode4; displayList(L1); System.out.println(); ListNode L2 = new ListNode(2); ListNode ynode1 = new ListNode(3); ListNode ynode2 = new ListNode(7); ListNode ynode3 = new ListNode(9); ListNode ynode4 = new ListNode(10); L2.next = ynode1; ynode1.next = ynode2; ynode2.next = ynode3; ynode3.next = ynode4; displayList(L2); System.out.println(); displayList(merageTwoLists(L1,L2));; } }