Skip to content

Commit 2f774b3

Browse files
committed
第一次提交
0 parents  commit 2f774b3

10 files changed

+166
-0
lines changed

.idea/.gitignore

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ListNodeReverse2.iml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
341 Bytes
Binary file not shown.
2.07 KB
Binary file not shown.
1.32 KB
Binary file not shown.

src/ListNode.java

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public class ListNode {
2+
int val;
3+
ListNode next;
4+
ListNode(int val) {
5+
this.val = val;
6+
this.next = null;
7+
}
8+
}

src/QuickSort.java

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
public class QuickSort {
2+
// 快速排序主函数
3+
public static void quickSort(int[] arr, int low, int high) {
4+
if (low < high) {
5+
// 获取分区索引
6+
int pi = partition(arr, low, high);
7+
8+
// 递归排序左半部分和右半部分
9+
quickSort(arr, low, pi - 1);
10+
quickSort(arr, pi + 1, high);
11+
}
12+
}
13+
14+
// 分区函数
15+
private static int partition(int[] arr, int low, int high) {
16+
// 选择最左边的元素作为基准值
17+
int pivot = arr[low];
18+
System.out.println("pivot="+pivot);
19+
int i = low + 1; // 从基准值的下一个元素开始
20+
int j = high;
21+
22+
while (true) {
23+
// 从左向右找到第一个大于等于基准值的元素
24+
while (i <= j && arr[i] < pivot) {
25+
i++;
26+
}
27+
28+
// 从右向左找到第一个小于等于基准值的元素
29+
while (i <= j && arr[j] > pivot) {
30+
j--;
31+
}
32+
33+
// 如果i和j相遇或交叉,退出循环
34+
if (i >= j) {
35+
break;
36+
}
37+
38+
// 交换arr[i]和arr[j]
39+
System.out.println("交换找到的最大值和最小值 。最大值 arr["+i+"]="+arr[i]+" , 最小值 arr["+j+"]="+ arr[j]);
40+
int temp = arr[i];
41+
arr[i] = arr[j];
42+
arr[j] = temp;
43+
System.out.println("交换完成 arr["+i+"]="+arr[i]+" , arr["+j+"]="+ arr[j]);
44+
System.out.print("中途结果: ");
45+
printArray(arr);
46+
// 继续移动指针
47+
i++;
48+
j--;
49+
}
50+
51+
// 将基准值放到正确的位置
52+
System.out.println(" arr["+low+"]="+arr[low]+" , arr["+j+"]="+ arr[j]);
53+
int temp = arr[low];
54+
arr[low] = arr[j];
55+
arr[j] = temp;
56+
57+
System.out.print("一轮结束: ");
58+
printArray(arr);
59+
// 返回基准值的索引
60+
return j;
61+
}
62+
63+
public static void main(String[] args) {
64+
int[] arr = {6, 3, 11, 25, 12, 2};
65+
System.out.println("排序前的数组:");
66+
printArray(arr);
67+
68+
quickSort(arr, 0, arr.length - 1);
69+
70+
System.out.println("排序后的数组:");
71+
printArray(arr);
72+
}
73+
74+
// 辅助方法:打印数组
75+
public static void printArray(int[] arr) {
76+
for (int i : arr) {
77+
System.out.print(i + " ");
78+
}
79+
System.out.println();
80+
}
81+
}

src/Solution.java

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
public class Solution {
2+
public ListNode reverseList(ListNode head) {
3+
ListNode prev = null;
4+
ListNode current = head;
5+
ListNode next = null;
6+
7+
while (current != null) {
8+
next = current.next; // 暂存当前节点的下一个节点
9+
current.next = prev; // 将当前节点的 next 指向前一个节点
10+
prev = current; // 将前一个节点指针向前移动
11+
current = next; // 将当前节点指针向前移动
12+
}
13+
14+
return prev; // 返回新的头节点
15+
}
16+
17+
public static void main(String[] args) {
18+
// 创建一个示例链表 1 -> 2 -> 3 -> 4 -> 5
19+
ListNode head = new ListNode(1);
20+
head.next = new ListNode(2);
21+
head.next.next = new ListNode(3);
22+
head.next.next.next = new ListNode(4);
23+
head.next.next.next.next = new ListNode(5);
24+
25+
// 打印反转之前的链表 反转前: 1 2 3 4 5
26+
// System.out.print("反转前: ");
27+
// while (head != null) {
28+
// System.out.print(head.val + " ");
29+
// head = head.next;
30+
// }
31+
// System.out.print("\n");
32+
33+
Solution solution = new Solution();
34+
ListNode reversedHead = solution.reverseList(head);
35+
36+
// 打印反转后的链表
37+
System.out.print("反转后: ");
38+
while (reversedHead != null) {
39+
System.out.print(reversedHead.val + " ");
40+
reversedHead = reversedHead.next;
41+
}
42+
System.out.print("\n");
43+
}
44+
}

0 commit comments

Comments
 (0)