Skip to content

Commit 1b5792f

Browse files
committed
sol 92 and 206, reverseBetween and reverseList, include the test project
1 parent bb6b420 commit 1b5792f

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

c_code/92_206_reverseBetween_test.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// 参考博文:
2+
// https://labuladong.gitbook.io/algo/shu-ju-jie-gou-xi-lie/shou-ba-shou-shua-lian-biao-ti-mu-xun-lian-di-gui-si-wei/di-gui-fan-zhuan-lian-biao-de-yi-bu-fen
3+
4+
// Definition for singly-linked list.
5+
struct ListNode {
6+
int val;
7+
struct ListNode *next;
8+
};
9+
10+
// 先实现整个链表反转 leetcode题206
11+
struct ListNode* reverseList(struct ListNode* head){
12+
if (head == NULL) return NULL; // 注意,要先判head是否为空
13+
if (head->next == NULL) return head;
14+
15+
struct ListNode* last = reverseList(head->next);
16+
head->next->next = head;
17+
head->next = NULL;
18+
return last;
19+
}
20+
21+
// 再实现前n个元素反转
22+
struct ListNode* succesor = NULL;
23+
struct ListNode* reverseN(struct ListNode* head, int n)
24+
{
25+
if (n == 1) {
26+
succesor = head->next;
27+
return head;
28+
}
29+
30+
struct ListNode* last = reverseN(head->next, n - 1); //只迭代展开一个,不要压栈出栈
31+
head->next->next = head;
32+
head->next = succesor;
33+
return last;
34+
}
35+
36+
// 再实现从第m到n的两侧均闭区间的反转
37+
struct ListNode* reverseBetween(struct ListNode* head, int left, int right){
38+
if (head == NULL) return NULL; // left/right值题目已做限定,默认是正确的
39+
40+
// 如果left=1,直接是起点
41+
if (left == 1) {
42+
return reverseN(head, right); // left移至1时,right大小等于n个数
43+
}
44+
// 否则,移动到对应起点
45+
head->next = reverseBetween(head->next, left - 1, right - 1); // 返回反转片段新的地点
46+
return head;
47+
}
48+
49+
50+
51+
//主函数调用
52+
int main()
53+
{
54+
struct ListNode head, p2, p3, p4, p5;
55+
head.val = 1;
56+
head.next = &p2;
57+
p2.val = 2;
58+
p2.next = &p3;
59+
p3.val = 3;
60+
p3.next = &p4;
61+
p4.val = 4;
62+
p4.next = &p5;
63+
p5.val = 5;
64+
p5.next = NULL;
65+
66+
// 初始化后遍历显示
67+
struct ListNode *cur, *pre, *nxt;
68+
cur = &head;
69+
while (cur != NULL) {
70+
printf("%d ",cur->val);
71+
cur = cur->next;
72+
}
73+
printf("\n");
74+
75+
// 反转后遍历显示
76+
// struct ListNode* newHead = reverseN(&head, 3);
77+
struct ListNode* newHead = reverseBetween(&head, 2, 5);
78+
cur = newHead;
79+
while (cur != NULL) {
80+
printf("%d ",cur->val);
81+
cur = cur->next;
82+
}
83+
printf("\n");
84+
85+
return 0;
86+
87+
}

c_code/92_reverseBetween.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
struct ListNode* reverseBetween(struct ListNode* head, int left, int right){
2+
if (head == NULL) return NULL; // left/right值题目已做限定,默认是正确的
3+
4+
// 如果left=1,直接是起点
5+
if (left == 1) {
6+
return reverseN(head, right); // left移至1时,right大小等于n个数
7+
}
8+
// 否则,移动到对应起点
9+
head->next = reverseBetween(head->next, left - 1, right - 1); // 返回反转片段新的地点
10+
return head;
11+
}

0 commit comments

Comments
 (0)