Skip to content

Commit 2ae50f5

Browse files
committed
新增链表部分
1 parent 88da39b commit 2ae50f5

File tree

17 files changed

+732
-2
lines changed

17 files changed

+732
-2
lines changed

alg-cpp.xcodeproj/project.pbxproj

+24
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,15 @@
182182
0922310F22FAFEEE00F0F5AF /* merge.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = merge.h; sourceTree = "<group>"; };
183183
0924B8E622F9D8A400435980 /* twoSum.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = twoSum.h; sourceTree = "<group>"; };
184184
0924B8E922F9E06000435980 /* removeDuplicates.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = removeDuplicates.h; sourceTree = "<group>"; };
185+
09255F962360A57300B38E3D /* getMaxRectSize.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = getMaxRectSize.h; sourceTree = "<group>"; };
185186
0925CDE722F67BB100DB3191 /* Permutation.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Permutation.h; sourceTree = "<group>"; };
186187
0929C38122FE7804006905B4 /* deleteDuplicates.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = deleteDuplicates.h; sourceTree = "<group>"; };
187188
0929C38422FE8DFE006905B4 /* getIntersectionNode.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = getIntersectionNode.h; sourceTree = "<group>"; };
188189
092A72A02358BA02005B0DC2 /* hanoiProblem.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = hanoiProblem.h; sourceTree = "<group>"; };
190+
092D77CF2371BF4C00CF2069 /* isPalindrome2.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = isPalindrome2.h; sourceTree = "<group>"; };
191+
09377EDC236A583C00222B54 /* ReverseLL.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ReverseLL.h; sourceTree = "<group>"; };
192+
09377EDF236A60E800222B54 /* ReversePartLL.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ReversePartLL.h; sourceTree = "<group>"; };
193+
093841F823673DDE00322A8B /* removeMidNode.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = removeMidNode.h; sourceTree = "<group>"; };
189194
093D6BFB22FBA0D000771DEC /* pascals_triangle.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = pascals_triangle.h; sourceTree = "<group>"; };
190195
093D6BFE22FBA5D200771DEC /* pascals_triangle_ii.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = pascals_triangle_ii.h; sourceTree = "<group>"; };
191196
093D6C0122FC704700771DEC /* best_time_to_buy_and_sell_stock.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = best_time_to_buy_and_sell_stock.h; sourceTree = "<group>"; };
@@ -219,9 +224,12 @@
219224
0947D1AE232AA674001DABA0 /* permutationsii.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = permutationsii.h; sourceTree = "<group>"; };
220225
094E2633233AF18200C08071 /* word_search.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = word_search.h; sourceTree = "<group>"; };
221226
094E2636233BC31600C08071 /* gray_code.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = gray_code.h; sourceTree = "<group>"; };
227+
0953CF0D2365DE7500F5B471 /* printCommonPart.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = printCommonPart.h; sourceTree = "<group>"; };
228+
0953CF102365E29A00F5B471 /* removeLastKthNode.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = removeLastKthNode.h; sourceTree = "<group>"; };
222229
095A524923364FC800D29259 /* permutation_sequence.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = permutation_sequence.h; sourceTree = "<group>"; };
223230
095A524C23371ADA00D29259 /* combinations.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = combinations.h; sourceTree = "<group>"; };
224231
095A524F2339193400D29259 /* subsets.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = subsets.h; sourceTree = "<group>"; };
232+
095E32C223706CCA00331B4A /* josephusKill.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = josephusKill.h; sourceTree = "<group>"; };
225233
095F60122322B3390072CF0C /* searchInRotatedSortedArrayII.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = searchInRotatedSortedArrayII.h; sourceTree = "<group>"; };
226234
0965B3CB23045219009A153E /* lengthOfLastWord.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = lengthOfLastWord.h; sourceTree = "<group>"; };
227235
0965B3CE23045B1C009A153E /* addBinary.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = addBinary.h; sourceTree = "<group>"; };
@@ -666,6 +674,20 @@
666674
path = "coding-interviews";
667675
sourceTree = "<group>";
668676
};
677+
0953CF0C2365DDEF00F5B471 /* itinterviews */ = {
678+
isa = PBXGroup;
679+
children = (
680+
0953CF0D2365DE7500F5B471 /* printCommonPart.h */,
681+
0953CF102365E29A00F5B471 /* removeLastKthNode.h */,
682+
093841F823673DDE00322A8B /* removeMidNode.h */,
683+
09377EDC236A583C00222B54 /* ReverseLL.h */,
684+
09377EDF236A60E800222B54 /* ReversePartLL.h */,
685+
095E32C223706CCA00331B4A /* josephusKill.h */,
686+
092D77CF2371BF4C00CF2069 /* isPalindrome2.h */,
687+
);
688+
path = itinterviews;
689+
sourceTree = "<group>";
690+
};
669691
0965B3D12304665C009A153E /* easy */ = {
670692
isa = PBXGroup;
671693
children = (
@@ -841,6 +863,7 @@
841863
09F082AF235768BD000F84C0 /* sortStackByStack.h */,
842864
092A72A02358BA02005B0DC2 /* hanoiProblem.h */,
843865
09EF0F4C235F43A4008E90DC /* getMaxWindow.h */,
866+
09255F962360A57300B38E3D /* getMaxRectSize.h */,
844867
);
845868
path = itinterviews;
846869
sourceTree = "<group>";
@@ -1063,6 +1086,7 @@
10631086
3A717D9D22DEC1A7002DA2C2 /* linkedList */ = {
10641087
isa = PBXGroup;
10651088
children = (
1089+
0953CF0C2365DDEF00F5B471 /* itinterviews */,
10661090
3A091A6422F3DDD800EFA79C /* coding-interviews */,
10671091
3A717DA622DEF32A002DA2C2 /* leetcode */,
10681092
3A717D9E22DEC1A7002DA2C2 /* main.cpp */,

linkedList.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1-
## 链表
1+
# 链表
2+
3+
### [打印两个有序链表的公共部分](./linkedList/itinterviews/printCommonPart.h)
4+
5+
### [<font color=red>在单链表和双链表中删除倒数第K个节点</font>](./linkedList/itinterviews/removeLastKthNode.h)
6+
### [删除链表的中间节点](./linkedList/itinterviews/removeMidNode.h)
7+
### [分别实现反转单向链表和反转双向链表](./linkedList/itinterviews/ReverseLL.h)
8+
### [<font color=red>反转部分单向链表</font>](./linkedList/itinterviews/ReversePartLL.h)
9+
10+
### [环形单链表的约瑟夫问题](./linkedList/itinterviews/josephusKill.h)
11+
12+
### [判断一个链表是否为回文结构](./linkedList/itinterviews/isPalindrome2.h)
13+
214
### leetcode
315

416
| &emsp;题号&emsp; | 题目链接&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;| 答案链接&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;| &emsp;难度&emsp; | &emsp;完成度&emsp; |

linkedList/coding-interviews/creatlist.h

+12
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ namespace codinginterviews {
2424
}
2525
return root;
2626
}
27+
DoubleNode *creatDoubleLists(const std::vector<int> &s){
28+
DoubleNode *root = new DoubleNode(0);
29+
DoubleNode *node = root;
30+
auto it = s.begin();
31+
while (it != s.end()) {
32+
node->next = new DoubleNode(*it);
33+
node->next->pre = node;
34+
node = node->next;
35+
it++;
36+
}
37+
return root;
38+
}
2739
}
2840

2941
#endif /* creatlist_hpp */

linkedList/itinterviews/ReverseLL.h

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//
2+
// ReverseLL.h
3+
// linkedList
4+
//
5+
// Created by junl on 2019/10/31.
6+
// Copyright © 2019 junl. All rights reserved.
7+
//
8+
9+
#ifndef ReverseLL_hpp
10+
#define ReverseLL_hpp
11+
12+
#include <stdio.h>
13+
#include "creatlist.h"
14+
/*
15+
分别实现反转单向链表和反转双向链表
16+
*/
17+
namespace itinterviews {
18+
class ReverseLL{
19+
public:
20+
ListNode *solveSL(ListNode *head){
21+
if (!head || !head->next) {
22+
return head;
23+
}
24+
ListNode *pre,*ct,*next;
25+
pre = head;
26+
ct = pre->next;
27+
pre->next = nullptr;
28+
while (ct) {
29+
next = ct->next;
30+
ct->next = pre;
31+
pre = ct;
32+
ct = next;
33+
}
34+
return pre;
35+
};
36+
37+
DoubleNode* solveDL(DoubleNode *head){
38+
if (!head || !head->next) {
39+
return head;
40+
}
41+
DoubleNode *pre,*ct,*next;
42+
pre = head;
43+
ct = pre->next;
44+
pre->next = nullptr;
45+
while (ct) {
46+
next = ct->next;
47+
ct->next = pre;
48+
pre->pre = ct;
49+
pre = ct;
50+
ct = next;
51+
}
52+
pre->pre = nullptr;
53+
return pre;
54+
}
55+
};
56+
57+
void test_ReverseLL(){
58+
std::cout << "--------test_ReverseLL---------" << std::endl;
59+
ListNode *head = codinginterviews::creatLists({1,2,3,4,5})->next;
60+
head->print();
61+
class ReverseLL so;
62+
so.solveSL(head)->print();
63+
64+
DoubleNode *dhead = codinginterviews::creatDoubleLists({1,2,3,4,5})->next;
65+
dhead->print();
66+
dhead = so.solveDL(dhead);
67+
dhead->print();
68+
69+
DoubleNode *tailNode = dhead;
70+
while (tailNode->next) {
71+
tailNode = tailNode->next;
72+
}
73+
std::cout << "验证双向链表反转过后的pre关系" << std::endl;
74+
while (tailNode) {
75+
std::cout << tailNode->val << ", ";
76+
tailNode = tailNode->pre;
77+
}
78+
std::cout << std::endl;
79+
}
80+
}
81+
#endif /* ReverseLL_hpp */
+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
//
2+
// ReversePartLL.h
3+
// linkedList
4+
//
5+
// Created by junl on 2019/10/31.
6+
// Copyright © 2019 junl. All rights reserved.
7+
//
8+
9+
#ifndef ReversePartLL_hpp
10+
#define ReversePartLL_hpp
11+
12+
#include <stdio.h>
13+
#include "creatlist.h"
14+
#include "ReverseLL.h"
15+
/*
16+
给定一个单向链表的头结点head,以及链各个整数from和to,在单链表上把第from个节点到第to个节点这一部分进行翻转.
17+
18+
1->2->3->4->5,from=2,to=4
19+
1->4->3->2->5
20+
21+
22+
1->2->3, from=1,to=3
23+
3->2->1
24+
25+
*/
26+
namespace itinterviews {
27+
class ReversePartLL{
28+
public:
29+
/*
30+
思路:
31+
将整个链表分成三部分:
32+
part1,part2,part3
33+
part1和part3不需要翻转,part2需要翻转,所以需要找到几个节点的位置:第一个是part1的末尾,第二个是part2的开始和结尾节点,最后把他们连接起来.
34+
*/
35+
ListNode *solve_bad(ListNode *head,int from,int to){
36+
if (!head || from < 1 || to < 1 || to <= from) {
37+
return head;
38+
}
39+
40+
ListNode *part1,*part2,*part3,*ct;
41+
part1 = part2 = part3 = nullptr;
42+
ct = head;
43+
int index = 1;
44+
while (ct) {
45+
//1~from-1这部分链表不会被反转
46+
if (index == from - 1) {
47+
part1 = ct;
48+
}else if (index == from){
49+
//这部分的链表需要被反转
50+
part2 = ct;
51+
}else if(index == to){
52+
//to后的不需要被反转
53+
part3 = ct->next;
54+
ct->next = nullptr;
55+
break;
56+
}
57+
ct = ct->next;
58+
index++;
59+
}
60+
ReverseLL r;
61+
62+
ListNode *part2Tail = part2;
63+
part2 = r.solveSL(part2);
64+
if (part1) {
65+
part1->next = part2;
66+
part2Tail->next = part3;
67+
return head;
68+
}else{
69+
part2Tail->next = part3;
70+
return part2;
71+
}
72+
};
73+
74+
ListNode *solve(ListNode *head,int from,int to){
75+
if (!head || from < 1 || to < 1 || to <= from) {
76+
return head;
77+
}
78+
ListNode *fPre,*tPos,*node;
79+
fPre = tPos = nullptr;
80+
node = head;
81+
int len = 0;
82+
while (node) {
83+
len++;
84+
if (len == from-1) {
85+
fPre = node;
86+
}else if (len == to + 1){
87+
tPos = node;
88+
}
89+
node = node->next;
90+
}
91+
92+
//fpre之前的不需要翻转,之后的需要翻转
93+
node = fPre ? fPre->next : head;
94+
ListNode *n1,*n2;
95+
n1 = node->next;
96+
node->next = tPos;
97+
while (n1 != tPos) {
98+
n2 = n1->next;
99+
n1->next = node;
100+
node = n1;
101+
n1 = n2;
102+
}
103+
if (fPre) {
104+
fPre->next = node;
105+
return head;
106+
}else{
107+
return node;
108+
}
109+
}
110+
};
111+
112+
113+
void test_ReversePartLL(){
114+
std::cout << "--------test_ReverseLL---------" << std::endl;
115+
ListNode *head = codinginterviews::creatLists({1,2,3,4,5})->next;
116+
head->print();
117+
class ReversePartLL so;
118+
so.solve(head,2,5)->print();
119+
120+
}
121+
}
122+
123+
#endif /* ReversePartLL_hpp */

0 commit comments

Comments
 (0)