如果人生有也能有第二版,我将会如何认真地修改校对!——克莱尔
直接上代码!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| 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; } } }
|