剑指3 从尾到头打印链表
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) :
* val(x), next(NULL) {
* }
* };
*/
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
vector<int> print;
stack<ListNode *> s;
ListNode * pNode = head;
while(pNode != nullptr)
{
s.push(pNode);
pNode = pNode->next;
}
while(!s.empty())
{
print.push_back(s.top().);
s.pop();
}
return print;
}
};
剑指14 链表中倒数第k个节点
JZ14 链表中倒数第k个节点
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
//双指针
if(pListHead == nullptr||k==0)
{
return nullptr;
}
ListNode * Ahead = pListHead;
ListNode * Behind = nullptr;
//第一个指针先走k-1步
for (int i = 0;i<k-1;i++)
{
if(Ahead->next !=nullptr)
Ahead = Ahead->next;
else
return nullptr;
}
Behind = pListHead;
while(Ahead->next != nullptr)
{
Ahead = Ahead->next;
Behind = Behind->next;
}
return Behind;
}
};
剑指15 反转链表
JZ15 反转链表
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
ListNode *pNode = pHead;
ListNode *pPre = nullptr;
ListNode *pReverHead = nullptr;
while (pNode != nullptr)
{
ListNode *pNext = pNode->next;
if(pNext ==nullptr)
{
pReverHead = pNode;
}
pNode->next = pPre;
pPre = pNode;
pNode = pNext;
}
return pReverHead;
}
};
剑指25 复杂链表的复制
/*
struct RandomListNode {
int label;
struct RandomListNode *next, *random;
RandomListNode(int x) :
label(x), next(NULL), random(NULL) {
}
};
*/
class Solution {
public:
RandomListNode* Clone(RandomListNode* pHead)
{
//复制节点
RandomListNode *pNode = pHead;
while(pNode !=nullptr)
{
RandomListNode * ClonedNode = new RandomListNode(pNode->label);
ClonedNode->next = pNode->next;
pNode->next = ClonedNode;
pNode = ClonedNode->next;
}
//设置随机指针
pNode = pHead;
RandomListNode *ClonedNode = pNode->next;
while(pNode !=nullptr)
{
if(pNode->random !=nullptr)
{
ClonedNode->random = pNode->random->next;
}
pNode = ClonedNode->next;
ClonedNode = pNode->next;
}
//奇偶分拆
pNode = pHead;
RandomListNode *ClonedHead = nullptr;
ClonedNode = nullptr;
if(pNode != nullptr)
{
ClonedHead = ClonedNode = pNode->next;
pNode->next = ClonedNode->next;
pNode = pNode->next;
}
while(pNode != nullptr)
{
ClonedNode->next = pNode->next;
ClonedNode = pNode->next;
pNode->next = ClonedNode->next;
pNode = ClonedNode->next;
}
return ClonedHead;
}
};
剑指36 两个链表的第一个公共结点
JZ36 两个链表的第一个公共结点
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2)
{
ListNode *l1 = pHead1;
ListNode *l2 = pHead2;
int length1 = 0;
int length2 = 0;
int lengdif = 0;
while (l1 != nullptr)
{
length1++;
l1 = l1->next;
}
while (l2 != nullptr)
{
length2++;
l2 = l2->next;
}
//判断哪个链表长
ListNode *Longlist ;
ListNode *Shortlist;
if(length1>length2)
{
Longlist = pHead1;
Shortlist = pHead2;
lengdif = length1 - length2;
}
else
{
Longlist = pHead2;
Shortlist = pHead1;
lengdif = length2 - length1;
}
//长链表先走lengdif步
for(int i = 0;i<lengdif;i++)
{
Longlist = Longlist->next;
}
//找节点
while(Longlist !=nullptr && Shortlist !=nullptr)
{
if(Longlist == Shortlist)
{
return Longlist;
}
Longlist = Longlist->next;
Shortlist = Shortlist->next;
}
}
};
剑指55 链表中环的入口结点
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
public:
ListNode* EntryNodeOfLoop(ListNode* pHead)
{
//先判断是否有环
ListNode *pNode = pHead;
ListNode *MeetNode = nullptr;
if(pNode == nullptr)
return nullptr;
ListNode *pSlow = pNode->next;
if(pSlow == nullptr)
return nullptr;
ListNode *pFast = pSlow->next;
while(pSlow !=nullptr &&pFast != nullptr)
{
if (pSlow == pFast)
{
MeetNode = pSlow;
break;
}
pSlow = pSlow->next;
pFast = pFast->next;
if(pFast != nullptr)
pFast = pFast->next;
}
//确定环的大小 存在length
if(MeetNode == nullptr)
return nullptr;
ListNode * pNode1 = MeetNode;
int length = 1;
while (pNode1->next != MeetNode)
{
pNode1 = pNode1->next;
++length;
}
pNode1 = pHead;
ListNode *pNode2 = pHead;
for(int i=0;i<length;i++)
{
pNode1 = pNode1->next;
}
while (pNode1 != nullptr && pNode2!=nullptr)
{
if(pNode1 == pNode2)
return pNode1;
pNode1 = pNode1->next;
pNode2 = pNode2->next;
}
}
};