反转单链表,用对数器测了100万组,绝对正确
package com.harrison.class02; import java.util.ArrayList; import java.util.List; public class Code01_ReverseLinkedList { public static class Node { public int value; public Node next; public Node(int v) { this.value = v; } } public static Node reverseLinkedList(Node head) { Node pre = null; Node next = null; while (head != null) { next = head.next; head.next = pre; pre = head; head = next; } return pre; } public static Node generateRandomLinkedList(int len, int value) { int size = (int) (Math.random() * (len + 1)); if (size == 0) { return null; } size--; Node head = new Node((int) (Math.random() * (value + 1))); Node pre = head; while (size != 0) { Node cur = new Node((int) (Math.random() * (value + 1))); pre.next = cur; pre = cur; size--; } return head; } public static List<Integer> getLinkedListOriginalOrder(Node head){ List<Integer> ans=new ArrayList<>(); while(head!=null) { ans.add(head.value); head=head.next; } return ans; } public static boolean checkedLinkedListReverse(List<Integer> origin,Node head) { for(int i=origin.size()-1; i>=0; i--) { if(!origin.get(i).equals(head.value)) { return false; } head=head.next; } return true; } public static Node testReverseLinkedList(Node head) { if(head==null) { return null; } ArrayList<Node> list=new ArrayList<>(); while(head!=null) { list.add(head); head=head.next; } list.get(0).next=null; int N=list.size(); for(int i=1; i<N; i++) { list.get(i).next=list.get(i-1); } return list.get(N-1); } public static void main(String[] args) { int len = 100; int value = 100; int testTimes = 1000000; System.out.println("test begin"); for (int i = 0; i < testTimes; i++) { Node node1=generateRandomLinkedList(len, value); List<Integer> list1=getLinkedListOriginalOrder(node1); node1=reverseLinkedList(node1); if(!checkedLinkedListReverse(list1, node1)) { System.out.println("Oops1!"); } Node node2=generateRandomLinkedList(len, value); List<Integer> list2=getLinkedListOriginalOrder(node2); node2=testReverseLinkedList(node2); if(!checkedLinkedListReverse(list2, node2)) { System.out.println("Oops2!"); } } System.out.println("finish"); } }