package class04;
import java.util.ArrayList;
import java.util.List;
public class Code01_ReverseList {
public static class Node {
public int value;
public Node next;
public Node(int data) {
value = data;
}
}
public static class DoubleNode {
public int value;
public DoubleNode last;
public DoubleNode next;
public DoubleNode(int data) {
value = data;
}
}
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 DoubleNode reverseDoubleList(DoubleNode head) {
DoubleNode pre = null;
DoubleNode next = null;
while (head != null) {
next = head.next;
head.next = pre;
head.last = next;
pre = head;
head = next;
}
return pre;
}
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 DoubleNode testReverseDoubleList(DoubleNode head) {
if (head == null) {
return null;
}
ArrayList<DoubleNode> list = new ArrayList<>();
while (head != null) {
list.add(head);
head = head.next;
}
list.get(0).next = null;
DoubleNode pre = list.get(0);
int N = list.size();
for (int i = 1; i < N; i++) {
DoubleNode cur = list.get(i);
cur.last = null;
cur.next = pre;
pre.last = cur;
pre = cur;
}
return list.get(N - 1);
}
// for test
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;
}
// for test
public static DoubleNode generateRandomDoubleList(int len, int value) {
int size = (int) (Math.random() * (len + 1));
if (size == 0) {
return null;
}
size--;
DoubleNode head = new DoubleNode((int) (Math.random() * (value + 1)));
DoubleNode pre = head;
while (size != 0) {
DoubleNode cur = new DoubleNode((int) (Math.random() * (value + 1)));
pre.next = cur;
cur.last = pre;
pre = cur;
size--;
}
return head;
}
// for test
public static List<Integer> getLinkedListOriginOrder(Node head) {
List<Integer> ans = new ArrayList<>();
while (head != null) {
ans.add(head.value);
head = head.next;
}
return ans;
}
// for test
public static boolean checkLinkedListReverse(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;
}
// for test
public static List<Integer> getDoubleListOriginOrder(DoubleNode head) {
List<Integer> ans = new ArrayList<>();
while (head != null) {
ans.add(head.value);
head = head.next;
}
return ans;
}
// for test
public static boolean checkDoubleListReverse(List<Integer> origin, DoubleNode head) {
DoubleNode end = null;
for (int i = origin.size() - 1; i >= 0; i--) {
if (!origin.get(i).equals(head.value)) {
return false;
}
end = head;
head = head.next;
}
for (int i = 0; i < origin.size(); i++) {
if (!origin.get(i).equals(end.value)) {
return false;
}
end = end.last;
}
return true;
}
public static void f(Node head) {
head = head.next;
}
// for test
public static void main(String[] args) {
int len = 50;
int value = 100;
int testTime = 100000;
System.out.println("test begin!");
for (int i = 0; i < testTime; i++) {
Node node1 = generateRandomLinkedList(len, value);
List<Integer> list1 = getLinkedListOriginOrder(node1);
node1 = reverseLinkedList(node1);
if (!checkLinkedListReverse(list1, node1)) {
System.out.println("Oops1!");
}
Node node2 = generateRandomLinkedList(len, value);
List<Integer> list2 = getLinkedListOriginOrder(node2);
node2 = testReverseLinkedList(node2);
if (!checkLinkedListReverse(list2, node2)) {
System.out.println("Oops2!");
}
DoubleNode node3 = generateRandomDoubleList(len, value);
List<Integer> list3 = getDoubleListOriginOrder(node3);
node3 = reverseDoubleList(node3);
if (!checkDoubleListReverse(list3, node3)) {
System.out.println("Oops3!");
}
DoubleNode node4 = generateRandomDoubleList(len, value);
List<Integer> list4 = getDoubleListOriginOrder(node4);
node4 = reverseDoubleList(node4);
if (!checkDoubleListReverse(list4, node4)) {
System.out.println("Oops4!");
}
}
System.out.println("test finish!");
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
数据结构-基于Java的算法和数据结构源码.zip (37个子文件)
src
class01
Code03_Sort.java 2KB
Code04_SelectionSort.java 2KB
Code01_PrintBinary.java 1KB
Code02_SumOfFactorial.java 578B
Code06_InsertionSort.java 3KB
Code05_BubbleSort.java 2KB
class06
Code01_MergeKSortedLists.java 1KB
Code04_MaximumDepthOfBinaryTree.java 459B
ShowComparator.java 3KB
Code05_ConstructBinaryTreeFromPreorderAndInorderTraversal.java 2KB
ShowComparator2.java 2KB
TraversalBinaryTree.java 1KB
Code03_SymmetricTree.java 571B
Code02_SameTree.java 486B
class05
Code03_BitAddMinusMultiDiv.java 1KB
Code01_BitMap1.java 1KB
Code02_BitMap2.java 1KB
class04
Code06_MergeTwoSortedLinkedList.java 817B
Code03_DoubleLinkedListToDeque.java 3KB
Code04_ReverseNodesInKGroup.java 1KB
Code01_ReverseList.java 5KB
Code02_LinkedListToQueueAndStack.java 4KB
Code05_AddTwoNumbers.java 1KB
class02
Code01_PreSum.java 654B
Code03_Comp.java 2KB
Code03_EqualProbabilityRandom.java 1KB
Code02_RandToRand.java 5KB
class03
Code03_BSNearRight.java 2KB
Code01_BSExist.java 1KB
Code04_BSAwesome.java 2KB
Code05_HashMapTreeMap.java 2KB
Code02_BSNearLeft.java 2KB
class07
Code03_PathSum.java 1KB
Code05_IsBinarySearchTree.java 2KB
Code01_BinaryTreeLevelOrderTraversalII.java 995B
Code04_PathSumII.java 1KB
Code02_BalancedBinaryTree.java 934B
共 37 条
- 1
资源评论
.whl
- 粉丝: 3762
- 资源: 4199
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- big data-峰会报告资源《png图片》
- big data-峰会报告资源(png图片)
- WebAssembly 技术分享与应用探索
- Wappalyzer 揭示网站上使用的技术
- python-基于python的抑郁症患者看护系统.zip
- python-django疫情数据可视化分析系统(lw+PPT).zip
- iPhone应用程序由主函数“main”启动,该函数负责调用UI应用程序主函数 此函数的形式
- CheckZong仅处理行数等于4的情况:检查从项目特征直接传递到下一个项目特征的行数,以确定它是否等于4,并且可以添加
- python-django基于python技术的学生管理系统的设计与开发.zip
- (1) 输入整数元素序列并创建序列表 (2) 实现序列表的遍历 (3) 在序列表中搜索某个元素,如果搜索成功
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功