如果人生有也能有第二版,我将会如何认真地修改校对!——克莱尔
直接上代码!
package com.ruben;import java.util.Comparator;import java.util.List;import java.util.concurrent.atomic.AtomicInteger;import java.util.function.Function;import java.util.stream.Collectors;import java.util.stream.Stream;public class ListNodeDemo { public static void main (String[] args) { ListNode zero = new ListNode(0 ); ListNode one = new ListNode(1 ); ListNode two = new ListNode(2 ); ListNode three = new ListNode(3 ); two.next = three; one.next = two; zero.next = one; ListNode tmp = zero; AtomicInteger length = new AtomicInteger(); while (tmp != null ) { length.getAndIncrement(); tmp = tmp.next; } List<Integer> integerList = Stream.iterate(zero, l -> l.next).limit(length.get()).mapToInt(l -> l.val).boxed().collect(Collectors.toList()); int [] ints = Stream.iterate(zero, l -> l.next).limit(length.get()).mapToInt(l -> l.val).toArray(); ListNode listNode = integerList.stream() .sorted(Comparator.reverseOrder()) .collect(() -> new ListNode(0 ), (listNode1, integer) -> { ListNode tmp1 = listNode1; while (tmp1.next != null ) { tmp1 = tmp1.next; } tmp1.next = new ListNode(integer); }, (listNode12, listNode2) -> Function.identity()); System.out.println(listNode.val); } public static class ListNode { int val; ListNode next; public ListNode (int x) { val = x; } } }