|
| 1 | +package leetcode.mergetwolinks; |
| 2 | + |
| 3 | +/** |
| 4 | + * <p></p> |
| 5 | + * |
| 6 | + * @author jwzhao |
| 7 | + * @version 1.0 |
| 8 | + * @date 2018/10/30 18:05 |
| 9 | + */ |
| 10 | +public class MergeTwoLinks { |
| 11 | + |
| 12 | + public static void main(String[] args){ |
| 13 | + MergeTwoLinks mergeTwoLinks = new MergeTwoLinks(); |
| 14 | + Integer[] strs1 = {0,2,3}; |
| 15 | + Integer[] strs2 = {1,3,4,6}; |
| 16 | + ListNode l1 = mergeTwoLinks.createLink(strs1); |
| 17 | + System.out.println("构建第一个有序链表是:"); |
| 18 | + mergeTwoLinks.display(l1); |
| 19 | + System.out.println(); |
| 20 | + ListNode l2 = mergeTwoLinks.createLink(strs2); |
| 21 | + System.out.println("构建第二个有序链表是:"); |
| 22 | + mergeTwoLinks.display(l2); |
| 23 | + System.out.println(); |
| 24 | + System.out.println("合并之后的链表是:"); |
| 25 | + System.out.println(); |
| 26 | + ListNode listNode = mergeTwoLinks.mergeTwoLists(l1,l2); |
| 27 | + mergeTwoLinks.display(listNode); |
| 28 | + } |
| 29 | + |
| 30 | + public ListNode mergeTwoLists(ListNode l1, ListNode l2) { |
| 31 | + int length1 = this.getLength(l1); |
| 32 | + int length2 = this.getLength(l2); |
| 33 | + ListNode currentNode = null; |
| 34 | + ListNode resultNode = null; |
| 35 | + if (length1 >= length2){ |
| 36 | + currentNode = l2; |
| 37 | + resultNode = l1; |
| 38 | + }else{ |
| 39 | + currentNode = l1; |
| 40 | + resultNode = l2; |
| 41 | + } |
| 42 | + |
| 43 | + while (currentNode != null){ |
| 44 | + int val = currentNode.val; |
| 45 | + resultNode = this.insertNode(resultNode,val); |
| 46 | + currentNode = currentNode.next; |
| 47 | + } |
| 48 | + return resultNode; |
| 49 | + } |
| 50 | + |
| 51 | + private ListNode insertNode(ListNode l1,int val){ |
| 52 | + ListNode currentNode = l1; |
| 53 | + if (currentNode == null){ |
| 54 | + return null; |
| 55 | + } |
| 56 | + int headVal = currentNode.val; |
| 57 | + if (headVal > val){ |
| 58 | + // 插入头结点 |
| 59 | + ListNode newHead = new ListNode(val); |
| 60 | + newHead.next = currentNode; |
| 61 | + currentNode = newHead; |
| 62 | + l1 = currentNode; |
| 63 | + return l1; |
| 64 | + } |
| 65 | + while (currentNode != null){ |
| 66 | + int currentVal = currentNode.val; |
| 67 | + if (currentNode.next == null){ |
| 68 | + // 插入尾节点 |
| 69 | + ListNode newNode = new ListNode(val); |
| 70 | + currentNode.next = newNode; |
| 71 | + return l1; |
| 72 | + } |
| 73 | + int nextVal = currentNode.next.val; |
| 74 | + if (currentVal <= val && val <= nextVal){ |
| 75 | + ListNode newNode = new ListNode(val); |
| 76 | + newNode.next = currentNode.next; |
| 77 | + currentNode.next = newNode; |
| 78 | + |
| 79 | + return l1; |
| 80 | + }else{ |
| 81 | + |
| 82 | + currentNode = currentNode.next; |
| 83 | + } |
| 84 | + } |
| 85 | + return l1; |
| 86 | + } |
| 87 | + |
| 88 | + private int getLength(ListNode listNode){ |
| 89 | + int length = 0; |
| 90 | + if (listNode == null){ |
| 91 | + return length; |
| 92 | + } |
| 93 | + |
| 94 | + while (listNode != null){ |
| 95 | + length++; |
| 96 | + listNode = listNode.next; |
| 97 | + } |
| 98 | + return length; |
| 99 | + } |
| 100 | + |
| 101 | + public void display(ListNode headNode){ |
| 102 | + ListNode currentNode = headNode; |
| 103 | + while (currentNode != null){ |
| 104 | + System.out.print(currentNode.val); |
| 105 | + if(currentNode.next != null){ |
| 106 | + System.out.print(" -> "); |
| 107 | + } |
| 108 | + currentNode = currentNode.next; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + public ListNode createLink(Integer[] strs){ |
| 113 | + ListNode listNode = null; |
| 114 | + for(Integer str : strs){ |
| 115 | + ListNode tempNode = new ListNode(str); |
| 116 | + if (listNode == null){ |
| 117 | + listNode = tempNode; |
| 118 | + }else{ |
| 119 | + ListNode nextNode = listNode; |
| 120 | + while (nextNode.next != null){ |
| 121 | + nextNode = nextNode.next; |
| 122 | + } |
| 123 | + nextNode.next = tempNode; |
| 124 | + } |
| 125 | + } |
| 126 | + return listNode; |
| 127 | + } |
| 128 | +} |
0 commit comments