#include
<iostream>
#include
<unordered_map>
#include
<stack>
using
namespace
std
;
struct
ListNode
{
int
val
;
ListNode
*
next
;
ListNode
(
int
x
)
:
val
(
x
)
,
next
(
nullptr
)
{
}
}
;
/**************/
//单链表的建立
ListNode
*
creat
(
)
{
ListNode
*
head
=
new
ListNode
(
-1
)
;
ListNode
*
p
=
head
;
ListNode
*
node
=
nullptr
;
int
x
=
0
;
int
cycle
=
1
;
while
(
cycle
)
{
cout
<<
"please input the data:"
<<
endl
;
cin
>>
x
;
if
(
x
!=
0
)
//当数据为0时,表示插入结束
{
node
=
new
ListNode
(
x
)
;
p
->
next
=
node
;
p
=
node
;
}
else
cycle
=
0
;
}
p
->
next
=
nullptr
;
//尾节点指针置为空
//head=head->next;
return
head
;
}
/*****************/
//单链表的打印
void
printList
(
ListNode
*
head
)
//note:这里head为单链表的头节点,而非第一个节点
{
if
(
head
->
next
==
nullptr
)
{
cout
<<
"list is empty!"
<<
endl
;
return
;
}
ListNode
*
p
=
head
->
next
;
int
index
=
0
;
while
(
p
!=
nullptr
)
{
cout
<<
"第"
<<++
index
<<
"个元素为:"
<<
p
->
val
<<
endl
;
p
=
p
->
next
;
}
}
/*********************/
//单链表的测长
int
getListLength
(
ListNode
*
head
)
//note:这里head为单链表的头节点,而非第一个节点
{
int
len
=
0
;
ListNode
*
p
=
head
->
next
;
while
(
p
!=
nullptr
)
{
++
len
;
p
=
p
->
next
;
}
return
len
;
}
/*************/
//单链表的插入
//将值为data的新节点插入到链表的第i(i=pos)个节点上
//单链表插入算法的时间耗费主要在查找第i-1个节点上;故单链表插入操作的复杂度为O(n);
//注;在链表中第i(i从0开始取值)个位置插入,分三种情况:插入到链表首部、中间、尾部
ListNode
*
insertList
(
ListNode
*
head
,
int
pos
,
int
data
)
//note:这里head为单链表的头节点,而非第一个节点
{
ListNode
*
newNode
=
new
ListNode
(
data
)
;
ListNode
*
p
=
head
;
int
index
=
1
;
while
(
p
!=
nullptr
&&
index
<
pos
)
{
p
=
p
->
next
;
++
index
;
}
newNode
->
next
=
p
->
next
;
p
->
next
=
newNode
;
return
head
;
}
/*******************/
//单链表的删除:将单链表中第i个节点删除
//删除单链表的头元素、中间元素、尾元素
//注:单链表的长度为n,则单链表删除第i个节点时,必须保证1=<i<=n,否则不合法。而当i=n+1
//时。虽然被删节点不存在,但其前驱节点存在,它是终端节点。因此被删节点的直接前驱存在并不
//意味着被删节点就一定存在,仅当p存在,且p不是终端节点同时满足index<i时,才能确定被删节点存在;
//算法的时间复杂度是O(n);
ListNode
*
deleteNode
(
ListNode
*
head
,
int
pos
)
//pos从1开始,1表示删除链表的头元素
{
ListNode
*
p
=
head
;
if
(
p
->
next
==
nullptr
)
//链表为空时
{
cout
<<
"链表为空"
<<
endl
;
return
nullptr
;
}
ListNode
*
node
=
nullptr
;
int
index
=
1
;
while
(
p
!=
nullptr
&&
index
<
pos
)
{
p
=
p
->
next
;
++
index
;
}
if
(
p
!=
nullptr
&&
p
->
next
!=
nullptr
)
{
node
=
p
->
next
;
p
->
next
=
node
->
next
;
delete
node
;
}
return
head
;
}
/*********************/
//查找链表中第i个节点
ListNode
*
searchNode
(
ListNode
*
head
,
int
pos
)
//这里head为链表的头节点,而非首元素
{
if
(
head
->
next
==
nullptr
||
pos
<
0
)
return
nullptr
;
if
(
pos
==
0
)
return
head
;
ListNode
*
p
=
head
->
next
;
while
(
--
pos
)
{
p
=
p
->
next
;
if
(
p
==
nullptr
)
{
cout
<<
"incorrect postion to search node!"
<<
endl
;
break
;
}
}
return
p
;
}
/**********************/
//查找单链表的中间节点
//方法一:先求链表的长度len,然后遍历len/2的距离即可查找到单链表的中间节点,需要遍历链表两次
//方法二:采用双指针,从链表头开始遍历,一个指针slow一次走一步,一个指针fast一次走两步,当fast
// 到达链表尾部时,slow恰好到达链表中间(fast到达链表尾部,当链表长度为奇数时,此时slow
// 指向的即为中间节点;当链表长度为偶数时,此时slow及slow的下一个元素均为中间节点)
ListNode
*
searchMidNode
(
ListNode
*
head
)
{
if
(
head
->
next
==
nullptr
)
return
head
;
ListNode
*
slow
=
head
->
next
;
ListNode
*
fast
=
head
->
next
;
while
(
fast
!=
nullptr
&&
fast
->
next
!=
nullptr
&&
fast
->
next
->
next
!=
nullptr
)
//若len为偶数,得到的slow和slow下一个均为mid
//while(fast!=nullptr&&fast->next!=nullptr)若len为偶数,得到的slow和slow上一个均为mid
{
slow
=
slow
->
next
;
fast
=
fast
->
next
->
next
;
}
return
slow
;
}
/*************************/
// 查找单链表中倒数第k个元素
//方法一:需遍历两次链表;先求链表的长度len,然后遍历len-k的距离即可查找到单链表的倒数第k个节点
//方法二:遍历一次链表,时间复杂度为O(n);设置两个指针p1、p2,让p2先走k-1步,然后p1、p2再同时走,
// 当p2走到链表尾时,p1所指位置就是所要找的节点
ListNode
*
searchReverseKthNode
(
ListNode
*
head
,
int
k
)
{
if
(
head
->
next
==
nullptr
||
k
==
0
)
return
head
;
ListNode
*
p1
=
head
->
next
;
ListNode
*
p2
=
head
->
next
;
while
(
p2
!=
nullptr
&&--
k
)
{
p2
=
p2
->
next
;
}
while
(
p2
->
next
!=
nullptr
)
{
p1
=
p1
->
next
;
p2
=
p2
->
next
;
}
return
p1
;
}
/********************/
//实现单链表反转;注意:当单链表有环时,无法反转链表,因为如果单链表有环,则存在两个节点指向同一个
// 节点。如果反转就变成一个节点指向两个节点了,这对于单链表是不可能的;
//方法一:
ListNode
*
reverse
(
ListNode
*
head
)
{
if
(
head
->
next
==
nullptr
&&
head
->
next
->
next
==
nullptr
)
return
head
;
ListNode
*
tail
=
head
->
next
;
ListNode
*
cur
=
tail
->
next
;
while
(
cur
!=
nullptr
)
{
//删除当前节点
tail
->
next
=
cur
->
next
;
//头插法,插入节点
cur
->
next
=
head
->
next
;
head
->
next
=
cur
;
//更新当前节点
cur
=
tail
->
next
;
}
return
head
;
}
//方法二:遍历一遍链表,利用一个辅助指针,存储遍历过程中当前指针指向的下一个元素,然后将当节点的指针
// 反转后,利用已存储的指针继续向后遍历;
ListNode
*
reverse2
(
ListNode
*
head
)
{
if
(
head
->
next
==
nullptr
)
return
head
;
ListNode
*
pre
=
head
->
next
;
ListNode
*
cur
=
pre
->
next
;
pre
->
next
=
nullptr
;
//原第一个节点为末节点;
ListNode
*
tmp
=
nullptr
;
while
(
cur
!=
nullptr
)
{
tmp
=
cur
->
next
;
cur
->
next
=
pre
;
pre
=
cur
;
cur
=
tmp
;
}
head
->
next
=
pre
;
// 新的第一个节点为原末节点
return
head
;
}
/****************/
//逆序输出单链表元素:
//方法一:从头到尾遍历节点,每经过一个节点的时候,把该节点放到一个栈中,当遍历完整个链表后,再从栈顶
// 输出节点的值;只需遍历一遍链表,但需维护一个额外的栈空间;
//方法二:在递归函数之后输出的当前元素,这样能确保输出第n个节点的元素语句永远在第n+1个递归函数之后执行,
// 也就是说第n个元素永远在第n+1个元素之后输出,最终先输出最后一个元素;
void
printListReversely
(
ListNode
*
head
)
{
if
(
head
->
next
!=
nullptr
)
{
printListReversely
(
head
->
next
)
;
cout
<<
head
->
next
->
val
<<
endl
;
}
}
/********************/
//将两个有序链表合并成一个有序链表
//方法一:递归;设两个链表的头结点分别为head1和head2,如果head1为空,则直接返回head2,;如果head2为空
// 则直接返回head1;如果head1链表的第一个数据小于head2链表的第一个数据,则把head1链表的
// 元素存储到新合并的链表中,递归遍历去掉第一个元素的head1链表和整个head2链表。如果head1
// 链表的第一个元素>=head2链表的第一个元素,则把head2链表的第一个元素存储到新合并的链表
// 中,递归遍历整个head1链表和去除第一个元素后的head2链表。直到两个链表的节点都被加入到
// 新合并的链表中。
// 递归终止条件:若head1为空,返回head2指针(head);若head2为空,返回head1指针(head);
// 递归方法所使用的栈空间与链表的长度成正比。
ListNode
*
mergeRecursive
(
ListNode
*
head1
,
ListNode
*
head2
)
//head1和head2均为链表的首节点,而非头结点
{
if
(
head1
==
nullptr
)
return
head2
;
if
(
head2
==
nullptr
)
return
head1
;
ListNode
*
newHead
=
nullptr
;
if
(
head1
->
val
<
head2
->
val
)
{
newHead
=
head1
;
newHead
->
next
=
mergeRecursive
(
head1
->
next
,
head2
)
;
}
else
{
newHead
=
head2
;
newHead
->
next
=
mergeRecursive
(
head1
,
head2
->
next
)
;
}
return
newHead
;
//返回首节点,而非头结点
}
// 方法二:非递归;分别用两个指针head1和head2遍历两个链表,如果当前head1指向的数据小于head2指向的数据
// 则将head1指向的节点归并入合并后的链表中;否则将head2指向的节点归并入合并后的链表中,
// 如果有一个链表遍历结束,则把未结束的链表连接到合并后的链表尾部;
ListNode
*
mergeList
(
ListNode
*
head1
,
ListNode
*
head2
)
//head1和head2均为链表的首节点,而非头结点
{
ListNode
*
newHead
=
new
ListNode
(
0
)
;
ListNode
*
node
=
newHead
;
while
(
head1
!=
nullptr
&&
head2
!=
nullptr
)
{
if
(
head1
->
val
<
head2
->
val
)
{
node
->
next
=
head1
;
node
=
head1
;
head1
=
head1
->
next
;
}
else
{
node
->
next
=
head2
;
node
=
head2
;
head2
=
head2
->
next
;
}
}
if
(
head1
!=
nullptr
)
{
node
->
next
=
head1
;
}
if
(
head2
!=
nullptr
)
{
node
->
next
=
head2
;
}
return
newHead
;
//返回头结点
}
/********************/
//删除无序单链表的重复元素
//方法一:使用hash表,时间复杂度O(n),空间复杂度O(n)
// 1、建立一个hash表,key为链表中已遍历的节点内容,开始时为空;
// 2、从头开始遍历链表中的节点
// 1)如果节点内容已经在hash中了,则删除此节点,继续向后遍历;
// 2)如果节点内容不在hash中,则保留此节点,将节点内容添加到hash中,继续遍历
ListNode
*
deleteDuplicateFromUnsortedList
(
ListNode
*
head
)
{
if
(
head
->
next
==
nullptr
&&
head
->
next
->
next
==
nullptr
)
return
head
;
unordered_map
<
int
,
bool
>
visited
(
false
)
;
ListNode
*
pre
=
head
;
ListNode
*
cur
=
pre
->
next
;
ListNode
*
tmp
=
nullptr
;
while
(
cur
!=
nullptr
)
{
if
(
visited
[
cur
->
val
])
{
tmp
=
cur
;
//tmp存储要删除的节点
pre
->
next
=
cur
->
next
;
//删除节点
cur
=
cur
->
next
;
delete
tmp
;
//释放删除节点的内存
}
else
{
visited
[
cur
->
val
]
=
true
;
pre
=
pre
->
next
;
cur
=
cur
->
next
;
}
}
return
head
;
}
//方法二:不使用额外空间,直接在原链表上进行操作,时间复杂度O(n^2)。
// 1、建立指针cur用于遍历链表;
// 2、建立指针p,p遍历cur之后的节点,并与cur的内容进行比较;
// 3、建立指针tmp,tmp保存要删除的节点,把需要删除的节点前后相连,删除重复节点
//非递归实现
ListNode
*
deleteDuplicateFromUnsortedList2
(
ListNode
*
head
)
{
if
(
head
->
next
==
nullptr
)
return
head
;
ListNode
*
cur
=
head
->
next
;
ListNode
*
p
=
nullptr
;
ListNode
*
tmp
=
nullptr
;
while
(
cur
!=
nullptr
)
{
p
=
cur
;
while
(
p
->
next
!=
nullptr
)
{
if
(
p
->
next
->
val
==
cur
->
val
)
{
tmp
=
p
->
next
;
p
->
next
=
p
->
next
->
next
;
delete
tmp
;
}
else
{
p
=
p
->
next
;
}
}
cur
=
cur
->
next
;
}
return
head
;
}
//递归实现
ListNode
*
deleteDuplicateFromUnsortedList3
(
ListNode
*
head
)
{
if
(
head
->
next
==
nullptr
)
return
head
;
ListNode
*
tmp
=
head
;
//递归过程中head不断变化,但初始时tmp都指向新的head;
head
->
next
=
deleteDuplicateFromUnsortedList3
(
head
->
next
)
;
//递归到head->next指向尾节点;此时head指向链表倒数第二个节点
ListNode
*
cur
=
head
->
next
;
//cur指向head的下一个节点;此时tmp=head;
while
(
cur
!=
nullptr
)
{
if
(
cur
->
val
==
head
->
val
)
//单次递归中,head是不变的,每次都把head的内容与head之后所有节点的内容进项比较,若相同,则删除此节点
{
tmp
->
next
=
cur
->
next
;
delete
cur
;
cur
=
tmp
->
next
;
}
else
{
cur
=
cur
->
next
;
tmp
=
tmp
->
next
;
//temp初始时是指向新的head的,之后作为临时变量,随着cur一起后移,始终作为cur的前驱节点,是为了当tmp节点的数据和head数据一样时,在删除cur节点时,用temp->next来保存cur的后继节点。
}
}
return
head
;
}
/*************************/
//判断是否有环
bool
hasCycle
(
ListNode
*
head
)
//head为链表的第一个节点,不是头节点
{
if
(
head
==
nullptr
)
return
false
;
ListNode
*
slow
=
head
;
ListNode
*
fast
=
head
;
while
(
fast
!=
nullptr
&&
fast
->
next
!=
nullptr
)
{
slow
=
slow
->
next
;
fast
=
fast
->
next
->
next
;
if
(
slow
==
fast
)
return
true
;
}
return
false
;
}
/**********************/
//寻找环的第一个节点(即环的入口)
ListNode
*
detectCycle
(
ListNode
*
head
)
//head为链表的第一个节点,不是头节点
{
if
(
head
==
nullptr
)
return
nullptr
;
ListNode
*
slow
=
head
;
ListNode
*
fast
=
head
;
ListNode
*
slow2
=
head
;
while
(
fast
!=
nullptr
&&
fast
->
next
!=
nullptr
)
{
slow
=
slow
->
next
;
fast
=
fast
->
next
->
next
;
if
(
fast
==
slow
)
{
while
(
slow
!=
slow2
)
{
slow
=
slow
->
next
;
slow2
=
slow2
->
next
;
}
return
slow2
;
}
}
return
nullptr
;
}
/*********************/
//当两个链表均无环时,求他们的第一个公共节点
//方法一:暴力搜索
ListNode
*
findFristCommonNode1
(
ListNode
*
head1
,
ListNode
*
head2
)
{
if
(
head1
->
next
==
nullptr
||
head2
->
next
==
nullptr
)
{
cout
<<
"存在空链表,无公共节点!"
<<
endl
;
return
nullptr
;
}
ListNode
*
p1
=
head1
->
next
;
ListNode
*
p2
=
head2
->
next
;
while
(
p1
!=
nullptr
)
{
while
(
p2
!=
nullptr
)
{
if
(
p1
==
p2
)
{
cout
<<
"存在公共节点!"
<<
endl
;
return
p1
;
}
else
p2
=
p2
->
next
;
}
p1
=
p1
->
next
;
}
return
nullptr
;
}
//方法二:利用单链表的性质+两个指针
ListNode
*
findFristCommonNode2
(
ListNode
*
head1
,
ListNode
*
head2
)
{
if
(
head1
->
next
==
nullptr
||
head2
->
next
==
nullptr
)
{
cout
<<
"存在空链表,无公共节点!"
<<
endl
;
return
nullptr
;
}
int
len1
=
getListLength
(
head1
)
;
int
len2
=
getListLength
(
head2
)
;
int
k
=
len1
-
len2
;
ListNode
*
plong
=
nullptr
;
ListNode
*
pshort
=
nullptr
;
if
(
k
>
0
)
{
plong
=
head1
->
next
;
pshort
=
head2
->
next
;
while
(
k
--
)
{
plong
=
plong
->
next
;
}
}
else
{
plong
=
head2
->
next
;
pshort
=
head1
->
next
;
k
=
(
-
k
)
;
while
(
k
--
)
{
plong
=
plong
->
next
;
}
}
while
(
plong
!=
nullptr
&&
pshort
!=
nullptr
&&
plong
!=
pshort
)
{
plong
=
plong
->
next
;
pshort
=
pshort
->
next
;
}
if
(
plong
==
pshort
)
return
plong
;
return
nullptr
;
}
//方法三:两个单链表首尾相连得到新链表,判断新链表是否存在环,若有环,则存在公共节点;否则,无公共节点;
ListNode
*
findFristCommonNode3
(
ListNode
*
head1
,
ListNode
*
head2
)
{
if
(
head1
->
next
==
nullptr
||
head2
->
next
==
nullptr
)
{
cout
<<
"存在空链表,无公共节点!"
<<
endl
;
return
nullptr
;
}
ListNode
*
p
=
head1
->
next
;
ListNode
*
newHead
=
p
;
while
(
p
->
next
!=
nullptr
)
{
p
=
p
->
next
;
}
p
->
next
=
head2
->
next
;
if
(
hasCycle
(
newHead
))
{
return
detectCycle
(
newHead
)
;
}
return
nullptr
;
}
//方法四:利用单链表的性质+两个栈辅助空间
ListNode
*
findFristCommonNode4
(
ListNode
*
head1
,
ListNode
*
head2
)
//note:这里head为单链表的头节点,而非第一个节点
{
if
(
head1
->
next
==
nullptr
||
head2
->
next
==
nullptr
)
{
cout
<<
"存在空链表,无公共节点!"
<<
endl
;
return
nullptr
;
}
stack
<
ListNode
*>
stack1
;
stack
<
ListNode
*>
stack2
;
ListNode
*
p1
=
head1
->
next
;
ListNode
*
p2
=
head2
->
next
;
ListNode
*
commonNode
=
nullptr
;
while
(
p1
!=
nullptr
)
{
stack1
.
push
(
p1
)
;
p1
=
p1
->
next
;
}
while
(
p2
!=
nullptr
)
{
stack2
.
push
(
p2
)
;
p2
=
p2
->
next
;
}
while
(
!
stack1
.
empty
(
)
&&!
stack2
.
empty
(
))
{
ListNode
*
node1
=
stack1
.
top
(
)
;
ListNode
*
node2
=
stack2
.
top
(
)
;
if
(
node1
==
node2
)
{
commonNode
=
node1
;
stack1
.
pop
(
)
;
stack2
.
pop
(
)
;
}
else
{
break
;
}
}
return
commonNode
;
}
/****************************/
//判断链表是否相交
bool
isHasCommonNode
(
ListNode
*
head1
,
ListNode
*
head2
)
//note:这里head为单链表的头节点,而非第一个节点
{
if
(
head1
->
next
==
nullptr
||
head2
->
next
==
nullptr
)
{
cout
<<
"存在空链表,无公共节点!"
<<
endl
;
return
false
;
}
bool
cycle1
=
hasCycle
(
head1
->
next
)
;
bool
cycle2
=
hasCycle
(
head2
->
next
)
;
if
((
cycle1
&&!
cycle2
)
||
(
!
cycle1
&&
cycle2
))
{
cout
<<
"两个链表一个有环,一个无环,故无公共节点!"
<<
endl
;
return
false
;
}
if
(
cycle1
&&
cycle2
)
{
ListNode
*
R1
=
detectCycle
(
head1
->
next
)
;
R1
->
next
=
nullptr
;
ListNode
*
R2
=
detectCycle
(
head2
->
next
)
;
R2
->
next
=
nullptr
;
ListNode
*
commonNode1
=
findFristCommonNode2
(
head1
,
head2
)
;
if
(
commonNode1
!=
nullptr
)
{
cout
<<
"两个均有环单链表存在公共节点!"
<<
endl
;
cout
<<
"第一个公共节点内容为:"
<<
commonNode1
->
val
<<
endl
;
return
true
;
}
else
{
cout
<<
"两个均有环单链表无公共节点!"
<<
endl
;
return
false
;
}
}
if
(
!
cycle1
&&!
cycle2
)
{
ListNode
*
commonNode2
=
findFristCommonNode2
(
head1
,
head2
)
;
if
(
commonNode2
!=
nullptr
)
{
cout
<<
"两个均无环单链表存在公共节点!"
<<
endl
;
cout
<<
"第一个公共节点内容为:"
<<
commonNode2
->
val
<<
endl
;
return
true
;
}
else
{
cout
<<
"两个均无环单链表无公共节点!"
<<
endl
;
return
false
;
}
}
return
false
;
}
/*********************/
int
main
(
)
{
/*
ListNode *head=new ListNode(0);
head=creat();
cout<<"输入的链表为:"<<endl;
printList(head);
int len=getListLength(head);
cout<<"单链表的长度为:"<<len<<endl;
int pos=1;
int data=50;
insertList(head,pos,data);
cout<<"插入后的链表为:"<<endl;
printList(head);
int i=5;
deleteNode(head, i);
cout<<"删除后的链表为:"<<endl;
printList(head);
int pos2=0;
ListNode *snode=searchNode(head,pos2);
cout<<"查找节点元素为;"<<snode->val<<endl;
ListNode *mid=searchMidNode(head);
cout<<"中间节点的元素为:"<<mid->val<<endl;
int k=1;
ListNode *kNode=searchReverseKthNode(head,k);
cout<<"倒数第k个元素为:"<<kNode->val<<endl;
ListNode *rhead=reverse(head);
cout<<"反转后的链表为:"<<endl;
printList(head);
ListNode *rhead=reverse2(head);
cout<<"反转后的链表为:"<<endl;
printList(head);
cout<<"反序打印列表:"<<endl;
printListReversely(head);
*/
/*
ListNode *head1=new ListNode(0);
head1=creat();
cout<<"输入的链表为:"<<endl;
printList(head1);
ListNode *head2=new ListNode(0);
head2=creat();
cout<<"输入的链表为:"<<endl;
printList(head2);
ListNode *newHead=new ListNode(0);
newHead=mergeRecursive(head1->next,head2->next);
cout<<"合并后的链表为:"<<endl;
ListNode *newHead1=new ListNode(0);
newHead1->next=newHead;
printList(newHead1);
*/
/*
ListNode *head3=new ListNode(0);
head3=creat();
cout<<"输入的链表为:"<<endl;
printList(head3);
ListNode *head4=new ListNode(0);
head4=creat();
cout<<"输入的链表为:"<<endl;
printList(head4);
ListNode *newHead2=new ListNode(0);
newHead2=mergeList(head3->next,head4->next);
cout<<"合并后的链表2为:"<<endl;
printList(newHead2);
*/
/*
ListNode *head5=new ListNode(0);
head5=creat();
cout<<"输入的链表为:"<<endl;
printList(head5);
ListNode *newHead5=new ListNode(0);
newHead5=deleteDuplicateFromUnsortedList(head5);
cout<<"去重后的链表为1:"<<endl;
printList(newHead5);
*/
/*
ListNode *head6=new ListNode(0);
head6=creat();
cout<<"输入的链表为:"<<endl;
printList(head6);
ListNode *newHead6=new ListNode(0);
newHead6=deleteDuplicateFromUnsortedList2(head6);
cout<<"去重后的链表为2:"<<endl;
printList(newHead6);
*/
/*
ListNode *head7=new ListNode(0);
head7=creat();
cout<<"输入的链表为:"<<endl;
printList(head7);
ListNode *newHead7=new ListNode(0);
newHead7=deleteDuplicateFromUnsortedList3(head7);
cout<<"去重后的链表为3:"<<endl;
printList(newHead7);
*/
/*
ListNode *head3=new ListNode(0);
head3=creat();
cout<<"输入的链表为:"<<endl;
printList(head3);
ListNode *head1=new ListNode(0);
head1=creat();
ListNode *newHead1=head1;
while(head1->next!=nullptr)
{
head1=head1->next;
}
head1->next=head3->next;
cout<<"输入的链表为:"<<endl;
printList(newHead1);
ListNode *head2=new ListNode(0);
head2=creat();
ListNode *newHead2=head2;
while(head2->next!=nullptr)
{
head2=head2->next;
}
head2->next=head3->next;
cout<<"输入的链表为:"<<endl;
printList(newHead2);
ListNode *newHead7=new ListNode(0);
newHead7=findFristCommonNode1(head1,head2);
if(newHead7!=nullptr)
cout<<"第一个公共节点为:"<<newHead7->val;
else
cout<<"no!"<<endl;
bool hasCom=isHasCommonNode(head1,head2);
if(hasCom)
cout<<"yes"<<endl;
else
cout<<"No!"<<endl;
*/
system
(
"pause"
)
;
return
0
;
}