|
| 1 | +package com.list; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | + |
| 6 | +/** |
| 7 | + * @ClassName: ListDemo |
| 8 | + * @Description: TODO |
| 9 | + * @Author yunfeng |
| 10 | + * @Date 2019-06-14 |
| 11 | + * @Version V1.0 |
| 12 | + **/ |
| 13 | +public class ListDemo { |
| 14 | + |
| 15 | + public static void main(String[] args) { |
| 16 | + ListNode listNode = new ListNode(1); |
| 17 | + ListNode listNode2 = new ListNode(2); |
| 18 | + ListNode listNode3= new ListNode(13); |
| 19 | + |
| 20 | + listNode.next = listNode2; |
| 21 | + listNode2.next = listNode3; |
| 22 | + |
| 23 | + MyList.travelList(listNode); |
| 24 | + //反转链表 |
| 25 | + ListNode nodes = reverseNode(listNode); |
| 26 | + |
| 27 | + MyList.travelList(nodes); |
| 28 | + |
| 29 | + //取中间值 |
| 30 | + ListNode midNode = getMid(nodes); |
| 31 | + |
| 32 | + System.out.println("midNode = " + midNode.value); |
| 33 | + |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testMerge(){ |
| 38 | + ListNode node1 = new ListNode(1); |
| 39 | + ListNode node2 = new ListNode(3); |
| 40 | + ListNode node3 = new ListNode(2); |
| 41 | + ListNode node4 = new ListNode(4); |
| 42 | + |
| 43 | + node1.next = node2; |
| 44 | + node3.next = node4; |
| 45 | + |
| 46 | +// ListNode head = mergeTwoList(node1,node3); |
| 47 | +// MyList.travelList(head); |
| 48 | + |
| 49 | + ListNode head2 = mergeTwoList2(node1,node3); |
| 50 | + MyList.travelList(head2); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * 反转数组 |
| 55 | + * @param head |
| 56 | + * @return |
| 57 | + */ |
| 58 | + public static ListNode reverseNode(ListNode head){ |
| 59 | + ListNode pre = null;//当前节点上一个节点 |
| 60 | + ListNode next = null;//当前节点下一个节点 |
| 61 | + while(head != null){ |
| 62 | + next = head.next; |
| 63 | + head.next = pre; |
| 64 | + pre = head; |
| 65 | + head = next; |
| 66 | + } |
| 67 | + return pre; |
| 68 | + |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * 取中间节点(偶数节点取中间节点前面一个节点) |
| 74 | + * 原理:快、慢指针 |
| 75 | + * @param head |
| 76 | + * @return |
| 77 | + */ |
| 78 | + public static ListNode getMid(ListNode head){ |
| 79 | + if(head == null){ |
| 80 | + return null; |
| 81 | + } |
| 82 | + |
| 83 | + ListNode fastNode = head; |
| 84 | + ListNode slowNode = head; |
| 85 | + while(fastNode.next != null && fastNode.next.next != null){ |
| 86 | + slowNode = slowNode.next; |
| 87 | + fastNode = fastNode.next.next; |
| 88 | + |
| 89 | + } |
| 90 | + return slowNode; |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + /** |
| 95 | + * 合并两个链表 -- 递归形式 |
| 96 | + * @param head1 |
| 97 | + * @param head2 |
| 98 | + * @return |
| 99 | + */ |
| 100 | + public static ListNode mergeTwoList(ListNode head1,ListNode head2){ |
| 101 | + if(head1 == null && head2 == null){ |
| 102 | + return null; |
| 103 | + } |
| 104 | + |
| 105 | + if(head1 == null){ |
| 106 | + return head2; |
| 107 | + } |
| 108 | + |
| 109 | + if(head2 == null){ |
| 110 | + return head1; |
| 111 | + } |
| 112 | + |
| 113 | + ListNode head = null; |
| 114 | + if(head1.value < head2.value){ |
| 115 | + head = head1; |
| 116 | + head.next = mergeTwoList(head1.next,head2); |
| 117 | + }else { |
| 118 | + head = head2; |
| 119 | + head.next = mergeTwoList(head1,head2.next); |
| 120 | + } |
| 121 | + |
| 122 | + return head; |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + /** |
| 128 | + * 合并两个链表 -- 循环形式 |
| 129 | + * @param head1 |
| 130 | + * @param head2 |
| 131 | + * @return |
| 132 | + */ |
| 133 | + public static ListNode mergeTwoList2(ListNode head1,ListNode head2){ |
| 134 | + if(head1 == null || head2 == null){ |
| 135 | + return head1 != null ? head1 : head2; |
| 136 | + } |
| 137 | + |
| 138 | + ListNode head = head1.value < head2.value ? head1 : head2; |
| 139 | + |
| 140 | + ListNode cur1 = head == head1 ? head1 : head2; |
| 141 | + ListNode cur2 = head == head1 ? head2 : head1; |
| 142 | + |
| 143 | + ListNode pre = null ;//cur1的前一个元素 |
| 144 | + ListNode next = null ;//cur2的前一个元素 |
| 145 | + |
| 146 | + while(cur1 != null && cur2 != null){ |
| 147 | + if(cur1.value < cur2.value){ |
| 148 | + pre = cur1; |
| 149 | + cur1 = cur1.next; |
| 150 | + }else { |
| 151 | + next = cur2.next; |
| 152 | + pre.next = cur2; |
| 153 | + cur2.next = cur1; |
| 154 | + pre = cur2; |
| 155 | + cur2 = next; |
| 156 | + } |
| 157 | + } |
| 158 | + pre.next = cur1 == null ? cur2 : cur1; |
| 159 | + |
| 160 | + return head; |
| 161 | + } |
| 162 | + |
| 163 | + |
| 164 | + |
| 165 | + /** |
| 166 | + * 链表奇数位升序,偶数位降序,合并两个链表,要求时间复杂度O(n) |
| 167 | + * 1、奇数和偶数位拆分为两个链表 |
| 168 | + * 2、偶数位链表反转 |
| 169 | + * 3、合并两个链表 |
| 170 | + * |
| 171 | + * @param head |
| 172 | + * @return |
| 173 | + */ |
| 174 | + public static ListNode[] splitNodeList(ListNode head){ |
| 175 | + ListNode head1 = null; |
| 176 | + ListNode head2 = null; |
| 177 | + |
| 178 | + ListNode cur1 = null; |
| 179 | + ListNode cur2 = null; |
| 180 | + |
| 181 | + int count = 1; |
| 182 | + while(head != null){ |
| 183 | + if(count % 2 == 1){ |
| 184 | + if(cur1 != null){ |
| 185 | + cur1.next = head; |
| 186 | + cur1 = cur1.next; |
| 187 | + }else { |
| 188 | + cur1 = head; |
| 189 | + head1 = cur1; |
| 190 | + } |
| 191 | + |
| 192 | + }else { |
| 193 | + if(cur2 != null){ |
| 194 | + cur2.next = head; |
| 195 | + cur2 = cur2.next; |
| 196 | + }else { |
| 197 | + cur2 = head; |
| 198 | + head2 = cur2; |
| 199 | + } |
| 200 | + |
| 201 | + } |
| 202 | + head = head.next; |
| 203 | + count++; |
| 204 | + } |
| 205 | + |
| 206 | + |
| 207 | + ListNode [] nodes = new ListNode[]{head1,head2}; |
| 208 | + |
| 209 | + return nodes; |
| 210 | + } |
| 211 | + |
| 212 | + @Test |
| 213 | + public void handleNode(){ |
| 214 | + ListNode node1 = new ListNode(1); |
| 215 | + ListNode node2 = new ListNode(4); |
| 216 | + ListNode node3 = new ListNode(3); |
| 217 | + ListNode node4 = new ListNode(2); |
| 218 | + |
| 219 | + node1.next = node2; |
| 220 | + node2.next = node3; |
| 221 | + node3.next = node4; |
| 222 | + |
| 223 | + ListNode [] nodes = splitNodeList(node1); |
| 224 | + |
| 225 | + ListNode head1 = nodes[0]; |
| 226 | + ListNode head2 = nodes[1]; |
| 227 | + |
| 228 | + head2 = reverseNode(head2); |
| 229 | + |
| 230 | + ListNode result = mergeTwoList(head1,head2); |
| 231 | + |
| 232 | + MyList.travelList(result); |
| 233 | + } |
| 234 | + |
| 235 | + |
| 236 | +} |
0 commit comments