/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pHead ListNode类
* @param k int整型
* @return ListNode类
*/
ListNode* FindKthToTail(ListNode* pHead, int k) {
// write code here
ListNode* fast=pHead;
ListNode* slow=pHead;
int ct=0;
while(ct<k){
if(fast==NULL)
return NULL;
fast=fast->next;
ct++;
}
while(fast!=NULL){
fast=fast->next;
slow=slow->next;
}
return slow;
}
};
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
class Solution {
public:
/**
*
* @param head ListNode类
* @param n int整型
* @return ListNode类
*/
ListNode* removeNthFromEnd(ListNode* head, int n) {
// write code here
ListNode* fast=head;
ListNode* slow=head;
ListNode* ft=new ListNode(-1);
ft->next=head;
ListNode* pre=ft;
int ct=0;
while(ct<n){
fast=fast->next;
ct++;
}
while(fast!=NULL){
fast=fast->next;
pre=slow;
slow=slow->next;
}
pre->next=slow->next;
return ft->next;
}
};
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
ListNode* pre=NULL;
ListNode* now=pHead;
ListNode* next=NULL;
while(now!=NULL){
next=now->next;
now->next=pre;
pre=now;
now=next;
}
pHead=pre;
return pHead;
}
};
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
class Solution {
public:
/**
*
* @param head ListNode类
* @param m int整型
* @param n int整型
* @return ListNode类
*/
ListNode* reverseBetween(ListNode* head, int m, int n) {
// write code here
ListNode* ft=new ListNode(-1);
ft->next=head;
ListNode* Mpre=ft;
for(int i=0;i<m-1;i++)
Mpre=Mpre->next;
ListNode* M=Mpre->next;
ListNode* pre=M;
ListNode* now=M->next;
ListNode* Next=NULL;
for(int i=m+1;i<=n;i++){
Next=now->next;
now->next=pre;
if(i==n){
M->next=Next;
Mpre->next=now;
}
pre=now;
now=Next;
}
return ft->next;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode* fast=head;
ListNode* slow=head;
while(slow!=NULL&&fast->next!=NULL&&fast->next->next!=NULL){
slow=slow->next;
fast=fast->next;
fast=fast->next;
if(fast==slow)
return true;
}
return false;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode* fast=head;
ListNode* slow=head;
while(slow!=NULL&&fast->next!=NULL&&fast->next->next!=NULL){
slow=slow->next;
fast=fast->next;
fast=fast->next;
if(fast==slow){
ListNode* ft=head;
while(ft!=fast){
ft=ft->next;
fast=fast->next;
}
return ft;
}
}
return NULL;
}
};
/*
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 cd1=0;
int cd2=0;
while(l1!=NULL||l2!=NULL){
if(l1!=NULL)
l1=l1->next;
else
cd2++;
if(l2!=NULL)
l2=l2->next;
else
cd1++;
}
l1=pHead1;
l2=pHead2;
while(cd1){
l1=l1->next;
cd1--;
}
while(cd2){
l2=l2->next;
cd2--;
}
while(l1!=l2){
l1=l1->next;
l2=l2->next;
}
return l1;
}
};
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
class Solution {
public:
/**
*
* @param l1 ListNode类
* @param l2 ListNode类
* @return ListNode类
*/
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
// write code here
if(l1==NULL)
return l2;
if(l2==NULL)
return l1;
ListNode* ans=new ListNode(-1);
ListNode* now=ans;
while(l1!=NULL||l2!=NULL){
if(l2==NULL||(l1!=NULL&&(l1->val)<=(l2->val))){
now->next=l1;
l1=l1->next;
}
else{
now->next=l2;
l2=l2->next;
}
now=now->next;
}
now->next=NULL;
return ans->next;
}
};