Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
cosmicing
Prj-Cpps
提交
a5f10cec
P
Prj-Cpps
项目概览
cosmicing
/
Prj-Cpps
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Prj-Cpps
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
a5f10cec
编写于
5月 14, 2021
作者:
cosmicing
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
上传新文件
上级
c151bf5c
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
342 addition
and
0 deletion
+342
-0
12-LinkList/33-MergeInBetween/mergeInBetween.cpp
12-LinkList/33-MergeInBetween/mergeInBetween.cpp
+342
-0
未找到文件。
12-LinkList/33-MergeInBetween/mergeInBetween.cpp
0 → 100644
浏览文件 @
a5f10cec
#include <iostream>
#include <string>
#include <vector>
#include<algorithm>
#include<sstream>
#include <cstring>
using
namespace
std
;
//Definition for singly-linked list.
struct
ListNode
{
int
val
;
ListNode
*
next
;
ListNode
(
int
x
)
:
val
(
x
),
next
(
NULL
)
{}
};
static
int
x
=
[]()
{
std
::
ios
::
sync_with_stdio
(
false
);
cin
.
tie
(
0
);
return
0
;
}();
class
Solution
{
public:
ListNode
*
detectCycle
(
ListNode
*
head
)
{
if
(
head
==
nullptr
)
return
nullptr
;
auto
fast
=
head
;
auto
slow
=
head
;
auto
entry
=
head
;
while
(
fast
->
next
and
fast
->
next
->
next
)
{
slow
=
slow
->
next
;
fast
=
fast
->
next
->
next
;
if
(
slow
==
fast
)
{
while
(
slow
!=
entry
)
{
slow
=
slow
->
next
;
entry
=
entry
->
next
;
}
return
entry
;
}
}
return
nullptr
;
}
ListNode
*
mergeInBetween
(
ListNode
*
list1
,
int
a
,
int
b
,
ListNode
*
list2
)
{
ListNode
*
p
=
list1
;
ListNode
*
q
=
list2
;
ListNode
*
resultList
;
ListNode
*
insertBegin
=
nullptr
,
*
insertEnd
=
nullptr
;
while
(
p
!=
nullptr
)
{
if
(
p
->
next
&&
p
->
next
->
val
==
a
)
{
insertBegin
=
p
;
}
if
(
p
->
val
==
b
&&
p
->
next
)
{
insertEnd
=
p
->
next
;
}
cout
<<
p
->
val
<<
endl
;
p
=
p
->
next
;
}
if
(
insertBegin
==
nullptr
)
{
resultList
=
list2
;
}
else
{
cout
<<
"insertBegin val:"
<<
insertBegin
->
val
<<
endl
;
resultList
=
list1
;
insertBegin
->
next
=
list2
;
}
while
(
q
->
next
!=
nullptr
)
{
q
=
q
->
next
;
cout
<<
q
->
val
<<
endl
;
}
if
(
insertEnd
!=
nullptr
)
{
cout
<<
"insertEnd val:"
<<
insertEnd
->
val
<<
endl
;
}
q
->
next
=
insertEnd
;
return
resultList
;
}
};
void
printList
(
ListNode
*
pNode
)
{
cout
<<
"LinkList traversal:"
<<
endl
;
while
(
pNode
!=
nullptr
)
{
cout
<<
pNode
->
val
<<
" "
;
pNode
=
pNode
->
next
;
}
cout
<<
endl
;
}
void
printList
(
ListNode
*
pNode
,
const
int
size
)
{
cout
<<
"LinkList traversal:"
<<
endl
;
int
cnt
=
size
;
while
(
pNode
!=
nullptr
&&
cnt
--
)
{
cout
<<
pNode
->
val
<<
" "
;
pNode
=
pNode
->
next
;
}
cout
<<
endl
;
}
void
trimLeftTrailingSpaces
(
string
&
input
)
{
input
.
erase
(
input
.
begin
(),
find_if
(
input
.
begin
(),
input
.
end
(),
[](
int
ch
)
{
return
!
isspace
(
ch
);
}));
}
void
trimRightTrailingSpaces
(
string
&
input
)
{
input
.
erase
(
find_if
(
input
.
rbegin
(),
input
.
rend
(),
[](
int
ch
)
{
return
!
isspace
(
ch
);
}).
base
(),
input
.
end
());
}
void
split
(
const
string
&
s
,
vector
<
string
>&
v
,
const
string
&
c
)
{
v
.
clear
();
string
::
size_type
pos1
=
0
,
pos2
=
s
.
find
(
c
);
while
(
string
::
npos
!=
pos2
)
{
v
.
push_back
(
s
.
substr
(
pos1
,
pos2
-
pos1
));
pos1
=
pos2
+
c
.
size
();
pos2
=
s
.
find
(
c
,
pos1
);
}
if
(
pos1
!=
s
.
length
()){
v
.
push_back
(
s
.
substr
(
pos1
));
}
}
string
StrProcess
(
string
str
)
{
int
pos1
=
str
.
find
(
"["
);
int
pos2
=
str
.
find
(
"]"
);
string
newStr
=
str
.
substr
(
pos1
+
1
,
pos2
-
pos1
-
1
);
//newStr.erase(std::remove(newStr.begin(), newStr.end(), ','), newStr.end());
return
newStr
;
}
vector
<
string
>
stringToVector
(
string
input
)
{
vector
<
string
>
inputVector
;
trimLeftTrailingSpaces
(
input
);
trimRightTrailingSpaces
(
input
);
input
=
input
.
substr
(
0
,
input
.
length
());
//list1 = [0,1,2,3,4,5],变为:0,1,2,3,4,5;
string
item
;
stringstream
ss
;
ss
.
str
(
input
);
getline
(
ss
,
item
,
','
);
inputVector
.
push_back
(
item
);
while
(
true
)
{
if
(
!
getline
(
ss
,
item
,
','
))
{
break
;
}
trimLeftTrailingSpaces
(
item
);
trimRightTrailingSpaces
(
item
);
//cout << item << endl;
inputVector
.
push_back
(
item
);
}
return
inputVector
;
}
vector
<
int
>
stringToIntegerVector
(
string
input
)
{
vector
<
int
>
output
;
trimLeftTrailingSpaces
(
input
);
trimRightTrailingSpaces
(
input
);
input
=
input
.
substr
(
0
,
input
.
length
());
stringstream
ss
;
ss
.
str
(
input
);
string
item
;
char
delim
=
','
;
while
(
getline
(
ss
,
item
,
delim
))
{
output
.
push_back
(
stoi
(
item
));
}
for
(
auto
ch
:
output
)
{
cout
<<
ch
<<
" "
;
}
cout
<<
endl
;
return
output
;
}
string
integerVectorToString
(
vector
<
int
>
list
,
int
length
=
-
1
)
{
if
(
length
==
-
1
)
{
length
=
list
.
size
();
}
if
(
length
==
0
)
{
return
"[]"
;
}
string
result
;
for
(
int
index
=
0
;
index
<
length
;
index
++
)
{
int
number
=
list
[
index
];
result
+=
to_string
(
number
)
+
", "
;
}
return
"["
+
result
.
substr
(
0
,
result
.
length
()
-
2
)
+
"]"
;
}
ListNode
*
stringToListNode
(
string
input
)
{
// Generate list from the input
vector
<
int
>
list
=
stringToIntegerVector
(
input
);
// Now convert that list into linked list
ListNode
*
dummyRoot
=
new
ListNode
(
0
);
ListNode
*
ptr
=
dummyRoot
;
for
(
int
item
:
list
)
{
ptr
->
next
=
new
ListNode
(
item
);
ptr
=
ptr
->
next
;
}
ptr
=
dummyRoot
->
next
;
delete
dummyRoot
;
return
ptr
;
}
ListNode
*
stringToCycleListNode
(
string
input
)
{
// Generate list from the input
vector
<
int
>
list
=
stringToIntegerVector
(
input
);
// Now convert that list into linked list
ListNode
*
dummyRoot
=
new
ListNode
(
0
);
ListNode
*
ptr
=
dummyRoot
;
ListNode
*
entry
=
dummyRoot
;
for
(
int
item
:
list
)
{
ptr
->
next
=
new
ListNode
(
item
);
ptr
=
ptr
->
next
;
if
(
item
==
2
)
{
entry
=
ptr
;
}
}
ptr
->
next
=
entry
;
ptr
=
dummyRoot
->
next
;
delete
dummyRoot
;
return
ptr
;
}
bool
hasCycle
(
ListNode
*
head
)
{
if
(
head
==
nullptr
)
return
false
;
auto
fast
=
head
;
auto
slow
=
head
;
while
(
fast
->
next
!=
nullptr
and
fast
->
next
->
next
!=
nullptr
)
{
slow
=
slow
->
next
;
fast
=
fast
->
next
->
next
;
if
(
slow
==
fast
)
return
true
;
}
return
false
;
}
string
listNodeToString
(
ListNode
*
node
)
{
if
(
node
==
nullptr
)
{
return
"[]"
;
}
string
result
;
while
(
node
)
{
//result += to_string(node->val) + ", ";
result
+=
to_string
(
node
->
val
)
+
","
;
node
=
node
->
next
;
}
return
"["
+
result
.
substr
(
0
,
result
.
length
()
-
1
)
+
"]"
;
}
int
main
()
{
string
line
;
string
inputline
;
string
list1
;
string
list2
;
int
a
,
b
;
while
(
getline
(
cin
,
line
))
{
vector
<
string
>
sv
;
split
(
line
,
sv
,
", "
);
for
(
const
auto
&
s
:
sv
)
{
cout
<<
s
<<
endl
;
if
(
strstr
(
s
.
c_str
(),
"list1"
)
!=
NULL
)
{
list1
=
StrProcess
(
s
);
}
if
(
strstr
(
s
.
c_str
(),
"list2"
)
!=
NULL
)
{
list2
=
StrProcess
(
s
);
}
if
(
strstr
(
s
.
c_str
(),
"a ="
)
!=
NULL
)
{
string
t
=
s
.
substr
(
s
.
find_last_of
(
'='
)
+
2
,
s
.
length
()
-
s
.
find_last_of
(
'='
)
-
1
);
//cout << t << endl;
a
=
stoi
(
t
);
}
if
(
strstr
(
s
.
c_str
(),
"b ="
)
!=
NULL
)
{
string
t
=
s
.
substr
(
s
.
find_last_of
(
'='
)
+
2
,
s
.
length
()
-
s
.
find_last_of
(
'='
)
-
1
);
//cout << t << endl;
b
=
stoi
(
t
);
}
}
cout
<<
list1
<<
endl
;
cout
<<
list2
<<
endl
;
ListNode
*
LinkList1
=
stringToListNode
(
list1
);
printList
(
LinkList1
);
ListNode
*
LinkList2
=
stringToListNode
(
list2
);
printList
(
LinkList2
);
//ListNode* ResultList = Solution().mergeInBetween(LinkList1, 0, 0, LinkList2);
ListNode
*
ResultList
=
Solution
().
mergeInBetween
(
LinkList1
,
a
,
b
,
LinkList2
);
//printList(ResultList);
string
out
=
listNodeToString
(
ResultList
);
cout
<<
out
<<
endl
;
#if 0
inputline = StrProcess(line);
cout << inputline << endl;
inputline = StrProcess(line);
cout << inputline << endl;
//ListNode* head = stringToListNode(inputline);
ListNode* head = stringToCycleListNode(inputline);
//printList(head);
printList(head,15);
if(hasCycle(head)) {
cout << "This is a cycle list" << endl;
} else {
cout << "This is not a cycle list" << endl;
}
ListNode* ret = Solution().detectCycle(head);
cout << "Entry node val:" << ret->val << endl;
return 0;
/*
string out = listNodeToString(ret);
cout << out << endl;
*/
#endif
}
return
0
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录