Skip to content

Commit 8ef30c7

Browse files
committed
新增链表
1 parent 2ae50f5 commit 8ef30c7

File tree

11 files changed

+780
-10
lines changed

11 files changed

+780
-10
lines changed

alg-cpp.xcodeproj/project.pbxproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@
176176
091A5E14231EB6FC000BEFAF /* merge_intervals.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = merge_intervals.h; sourceTree = "<group>"; };
177177
091A5E17231EC2AE000BEFAF /* sort_colors.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = sort_colors.h; sourceTree = "<group>"; };
178178
091B8A7122F48FD50050FB4C /* IsBalanced_Solution.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = IsBalanced_Solution.h; sourceTree = "<group>"; };
179+
091E1CED238EBFB100680649 /* reverseKNodes.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = reverseKNodes.h; sourceTree = "<group>"; };
179180
0922310622FA445300F0F5AF /* removeElement.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = removeElement.h; sourceTree = "<group>"; };
180181
0922310922FA497500F0F5AF /* searchInsert.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = searchInsert.h; sourceTree = "<group>"; };
181182
0922310C22FA50DD00F0F5AF /* maxSubArray.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = maxSubArray.h; sourceTree = "<group>"; };
@@ -199,6 +200,8 @@
199200
09421FFA23142AEB00A7BA67 /* searchRange.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = searchRange.h; sourceTree = "<group>"; };
200201
09421FFD231583FC00A7BA67 /* isValidSudoku.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = isValidSudoku.h; sourceTree = "<group>"; };
201202
0942542C232942D3006B83E9 /* gas_station.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = gas_station.h; sourceTree = "<group>"; };
203+
0943DB3A2382E20100E995BB /* addList.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = addList.h; sourceTree = "<group>"; };
204+
0943DB3D2382EDEE00E995BB /* getIntersectNode.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = getIntersectNode.h; sourceTree = "<group>"; };
202205
094524B7234790CD00CCBFB9 /* coinChange.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = coinChange.h; sourceTree = "<group>"; };
203206
094524BA234A32D000CCBFB9 /* levenshtein_distance.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = levenshtein_distance.h; sourceTree = "<group>"; };
204207
0946B01522F68BA50043469D /* FirstNotRepeatingChar.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FirstNotRepeatingChar.h; sourceTree = "<group>"; };
@@ -263,6 +266,7 @@
263266
099EAEF822FFC4C5006437BD /* palindrome_number.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = palindrome_number.h; sourceTree = "<group>"; };
264267
09A208B02308D96E00094088 /* zigzag_conversion.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = zigzag_conversion.h; sourceTree = "<group>"; };
265268
09A208B32309990C00094088 /* string_to_integer_atoi.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = string_to_integer_atoi.h; sourceTree = "<group>"; };
269+
09A3F5C2237A358600BE6839 /* copyListWithRand.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = copyListWithRand.h; sourceTree = "<group>"; };
266270
09A7AC1D234E34EA00DFECF0 /* matrix_move.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = matrix_move.h; sourceTree = "<group>"; };
267271
09ABED832300E22800113589 /* romanToInt.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = romanToInt.h; sourceTree = "<group>"; };
268272
09ABED872300EE0900113589 /* longestCommonPrefix.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = longestCommonPrefix.h; sourceTree = "<group>"; };
@@ -296,6 +300,7 @@
296300
09E18E5D22F58301002D0227 /* GetNumberOfK.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GetNumberOfK.h; sourceTree = "<group>"; };
297301
09E18E6022F588E8002D0227 /* FindNumsAppearOnce.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FindNumsAppearOnce.h; sourceTree = "<group>"; };
298302
09E18E6422F59065002D0227 /* replaceSpace.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = replaceSpace.h; sourceTree = "<group>"; };
303+
09E8C4112379A3AC006E03ED /* listPartition.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = listPartition.h; sourceTree = "<group>"; };
299304
09EF0F4C235F43A4008E90DC /* getMaxWindow.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = getMaxWindow.h; sourceTree = "<group>"; };
300305
09F082AF235768BD000F84C0 /* sortStackByStack.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = sortStackByStack.h; sourceTree = "<group>"; };
301306
09F2A5E52354CF320026F4A2 /* TwoStacksQueue.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TwoStacksQueue.h; sourceTree = "<group>"; };
@@ -684,6 +689,11 @@
684689
09377EDF236A60E800222B54 /* ReversePartLL.h */,
685690
095E32C223706CCA00331B4A /* josephusKill.h */,
686691
092D77CF2371BF4C00CF2069 /* isPalindrome2.h */,
692+
09E8C4112379A3AC006E03ED /* listPartition.h */,
693+
09A3F5C2237A358600BE6839 /* copyListWithRand.h */,
694+
0943DB3A2382E20100E995BB /* addList.h */,
695+
0943DB3D2382EDEE00E995BB /* getIntersectNode.h */,
696+
091E1CED238EBFB100680649 /* reverseKNodes.h */,
687697
);
688698
path = itinterviews;
689699
sourceTree = "<group>";
-282 Bytes
Binary file not shown.
8.25 KB
Binary file not shown.

alg-cpp.xcodeproj/xcuserdata/junl.xcuserdatad/xcschemes/xcschememanagement.plist

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,22 @@
2222
<key>base.xcscheme_^#shared#^_</key>
2323
<dict>
2424
<key>orderHint</key>
25-
<integer>5</integer>
25+
<integer>7</integer>
2626
</dict>
2727
<key>bsearch.xcscheme_^#shared#^_</key>
2828
<dict>
2929
<key>orderHint</key>
30-
<integer>9</integer>
30+
<integer>8</integer>
3131
</dict>
3232
<key>divideandconquer.xcscheme_^#shared#^_</key>
3333
<dict>
3434
<key>orderHint</key>
35-
<integer>14</integer>
35+
<integer>13</integer>
3636
</dict>
3737
<key>dp.xcscheme_^#shared#^_</key>
3838
<dict>
3939
<key>orderHint</key>
40-
<integer>12</integer>
40+
<integer>10</integer>
4141
</dict>
4242
<key>greed.xcscheme_^#shared#^_</key>
4343
<dict>
@@ -47,12 +47,12 @@
4747
<key>hasTable.xcscheme_^#shared#^_</key>
4848
<dict>
4949
<key>orderHint</key>
50-
<integer>10</integer>
50+
<integer>11</integer>
5151
</dict>
5252
<key>linkedList.xcscheme_^#shared#^_</key>
5353
<dict>
5454
<key>orderHint</key>
55-
<integer>6</integer>
55+
<integer>9</integer>
5656
</dict>
5757
<key>other.xcscheme_^#shared#^_</key>
5858
<dict>
@@ -62,17 +62,17 @@
6262
<key>recursion.xcscheme_^#shared#^_</key>
6363
<dict>
6464
<key>orderHint</key>
65-
<integer>13</integer>
65+
<integer>14</integer>
6666
</dict>
6767
<key>sort.xcscheme_^#shared#^_</key>
6868
<dict>
6969
<key>orderHint</key>
70-
<integer>8</integer>
70+
<integer>5</integer>
7171
</dict>
7272
<key>stack+queue.xcscheme_^#shared#^_</key>
7373
<dict>
7474
<key>orderHint</key>
75-
<integer>7</integer>
75+
<integer>6</integer>
7676
</dict>
7777
<key>string.xcscheme_^#shared#^_</key>
7878
<dict>
@@ -82,7 +82,7 @@
8282
<key>tree.xcscheme_^#shared#^_</key>
8383
<dict>
8484
<key>orderHint</key>
85-
<integer>11</integer>
85+
<integer>12</integer>
8686
</dict>
8787
</dict>
8888
<key>SuppressBuildableAutocreation</key>

linkedList.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111

1212
### [判断一个链表是否为回文结构](./linkedList/itinterviews/isPalindrome2.h)
1313

14+
### [将单向链表按某值划分成左边小,中间相等,右边大的形式](./linkedList/itinterviews/listPartition.h)
15+
16+
### [复制含有随机指针节点的链表](./linkedList/itinterviews/copyListWithRand.h)
17+
18+
### [ 两个单链表生成相加链表](./linkedList/itinterviews/addList.h)
19+
### [ <font color=red>两个单链表相交的一系列问题</font>](./linkedList/itinterviews/getIntersectNode.h)
20+
21+
### [<font color=red>将单链表的每K个节点之间逆序</font>](./linkedList/itinterviews/reverseKNodes.h)
22+
23+
1424
### leetcode
1525

1626
| &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/itinterviews/addList.h

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
//
2+
// addList.h
3+
// linkedList
4+
//
5+
// Created by junl on 2019/11/18.
6+
// Copyright © 2019 junl. All rights reserved.
7+
//
8+
9+
#ifndef addList_hpp
10+
#define addList_hpp
11+
12+
#include <stdio.h>
13+
#include "creatlist.h"
14+
#include <stack>
15+
/*
16+
两个单链表生成相加链表
17+
例如:链表1位9->3->7,链表2位6->3,结果链表为1->0->0->0
18+
*/
19+
namespace itinterviews {
20+
class addList{
21+
public:
22+
/*
23+
思路:利用栈后进先出的结构O(N+M),O(N+M)
24+
*/
25+
ListNode *solve(ListNode * head1, ListNode *head2){
26+
if (!head1 || !head2)
27+
return nullptr;
28+
std::stack<int> stk1;
29+
std::stack<int> stk2;
30+
ListNode *node = head1;
31+
while (node) {
32+
stk1.push(node->val);
33+
node=node->next;
34+
}
35+
node = head2;
36+
while (node) {
37+
stk2.push(node->val);
38+
node=node->next;
39+
}
40+
int flag=0;
41+
ListNode *pre = nullptr;
42+
node = nullptr;
43+
while (!stk1.empty() || !stk2.empty()) {
44+
int n1= 0, n2= 0;
45+
if (!stk1.empty()) {
46+
n1 = stk1.top();
47+
stk1.pop();
48+
}
49+
if (!stk2.empty()){
50+
n2 = stk2.top();
51+
stk2.pop();
52+
}
53+
int n = n1+n2+flag;
54+
flag = n / 10;
55+
pre = node;
56+
node = new ListNode(n%10);
57+
node->next = pre;
58+
}
59+
if (flag){
60+
pre = node;
61+
node = new ListNode(flag);
62+
node->next = pre;
63+
}
64+
return node;
65+
}
66+
};
67+
68+
/*
69+
思路:
70+
通过反转两个链表,可以节省掉栈空间的消耗
71+
*/
72+
class addList2{
73+
public:
74+
ListNode *solve(ListNode *head1,ListNode *head2){
75+
if (!head1 || !head2)
76+
return nullptr;
77+
head1 = reverse(head1);
78+
head2 = reverse(head2);
79+
ListNode *ct,*pre;
80+
ct = pre = nullptr;
81+
int co=0;
82+
ListNode *node1 = head1;
83+
ListNode *node2 = head2;
84+
while (node1 || node2) {
85+
int n1 = 0;
86+
int n2 = 0;
87+
if (node1){
88+
n1 = node1->val;
89+
node1 = node1->next;
90+
}
91+
if (node2){
92+
n2 = node2->val;
93+
node2 = node2->next;
94+
}
95+
int n = n1 + n2 + co;
96+
co = n/10;
97+
pre = ct;
98+
ct = new ListNode(n%10);
99+
ct->next = pre;
100+
}
101+
if (co){
102+
pre = ct;
103+
ct = new ListNode(co);
104+
ct->next = pre;
105+
}
106+
head1 = reverse(head1);
107+
head2 = reverse(head2);
108+
return ct;
109+
}
110+
private:
111+
ListNode* reverse(ListNode *head){
112+
ListNode *pre,*ct,*next;
113+
pre=head;
114+
ct=pre->next;
115+
pre->next=nullptr;
116+
while (ct) {
117+
next = ct->next;
118+
ct->next = pre;
119+
pre = ct;
120+
ct = next;
121+
}
122+
return pre;
123+
}
124+
125+
};
126+
127+
void test_addList(){
128+
std::cout << "---两个单链表生成相加链表---------" << std::endl;
129+
// class addList so;
130+
class addList2 so;
131+
ListNode *head1 = codinginterviews::creatLists({9,3,7})->next;
132+
ListNode *head2 = codinginterviews::creatLists({6,3})->next;
133+
so.solve(head1, head2)->print();
134+
}
135+
}
136+
137+
#endif /* addList_hpp */
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
//
2+
// copyListWithRand.h
3+
// linkedList
4+
//
5+
// Created by junl on 2019/11/12.
6+
// Copyright © 2019 junl. All rights reserved.
7+
//
8+
9+
#ifndef copyListWithRand_hpp
10+
#define copyListWithRand_hpp
11+
12+
#include <stdio.h>
13+
#include "creatlist.h"
14+
#include <map>
15+
/*
16+
复制含有随机指针节点的链表.
17+
18+
进阶:不使用额外的数据结构,只用有限的几个变量,且在时间复杂度为O(n)内完成。
19+
总结:
20+
21+
两种解析关键点在于:如果通过原节点找到copy的新节点,这样才能设置rand节点值。方法一是通过hashmap,方法二比较取巧,通过构建一个特殊链表.
22+
*/
23+
namespace itinterviews {
24+
class RNode{
25+
public:
26+
int val;
27+
RNode *next;
28+
RNode *rand;
29+
RNode() = delete;
30+
RNode(int value):val(value),next(nullptr),rand(nullptr){};
31+
};
32+
/*
33+
思路: 关键点在于如何处理rand节点,rand节点是不能new出来的,不然就和新链表的节点对应不上。
34+
处理: 借助hashmap,O(N),O(N)
35+
*/
36+
class copyListWithRand{
37+
public:
38+
RNode *solve(RNode *head){
39+
if (!head)
40+
return head;
41+
std::map<RNode *,RNode *> m;
42+
RNode *node = head;
43+
//一一映射保存到map中,此时next和rand都还没赋值
44+
while (node) {
45+
m[node] = new RNode(node->val);
46+
node = node->next;
47+
}
48+
node = head;
49+
while (node) {
50+
m[node]->next = m[node->next];
51+
m[node]->rand = m[node->rand];
52+
node = node->next;
53+
}
54+
return m[head];
55+
};
56+
};
57+
/*
58+
思路:比如原链表为1->2->3,构建出新链接为1-> 1‘ -> 2 -> 2' -> 3-> 3',这样可以非常容易的找到原链表节点和新链表节点之间的关系.即:
59+
node源->next即为node新
60+
*/
61+
class copyListWithRand_advance{
62+
public:
63+
RNode *solve(RNode *head){
64+
if (!head)
65+
return head;
66+
RNode *node = head;
67+
RNode *next;
68+
//构建1-> 1‘ -> 2 -> 2' -> 3-> 3'
69+
while (node) {
70+
next = node->next;
71+
node->next = new RNode(node->val);
72+
node->next->next = next;
73+
node = next;
74+
}
75+
76+
//处理rand节点
77+
node = head;
78+
while (node) {
79+
next = node->next->next;//跳过复制的节点
80+
if (node->rand){
81+
node->next->rand = node->rand->next;
82+
}
83+
node = next;
84+
}
85+
//删除多余节点
86+
RNode *result = head->next;
87+
node = head;
88+
RNode *copyNode;
89+
while (node) {
90+
next = node->next->next;
91+
copyNode = node->next;
92+
node->next = next;
93+
copyNode->next = next ? next->next : nullptr;
94+
node = node->next;
95+
}
96+
return result;
97+
}
98+
};
99+
100+
void test_copyListWithRand(){
101+
std::cout << "------复制含有随机指针节点的链表.----------" << std::endl;
102+
RNode *head = new RNode(1);
103+
RNode *head2 = new RNode(2);
104+
RNode *head3 = new RNode(3);
105+
head->next = head2;
106+
head->rand = head3;
107+
head2->next = head3;
108+
head3->rand = head;
109+
110+
// class copyListWithRand so;
111+
class copyListWithRand_advance so;
112+
head = so.solve(head);
113+
head;
114+
}
115+
}
116+
117+
118+
#endif /* copyListWithRand_hpp */

0 commit comments

Comments
 (0)