Leetcode9. 删除链表的倒数第N个节点
Leetcode83. 删除排序链表中的重复元素
Leetcode61. 旋转链表
Leetcode24. 两两交换链表中的节点
Leetcode206. 反转链表
Leetcode92. 反转链表 II
Leetcode160. 相交链表
Leetcode142. 环形链表 II
Leetcode9. 删除链表的倒数第N个节点
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode *fakehead = new ListNode(-1);
fakehead->next = head;
ListNode *slow,*fast;
slow = fakehead;
fast = fakehead;
while(n--){
fast = fast->next;
}
while(fast->next){
slow = slow->next;
fast = fast->next;
}
slow->next = slow->next->next;
return fakehead->next;
}
};
Leetcode83. 删除排序链表中的重复元素
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(!head) return NULL;
ListNode *p = head;
ListNode *q;
while(p->next){
q = p->next;
if(q->val==p->val) p->next = q->next;
else p = q;
}
return head;
}
};
Leetcode61. 旋转链表
class Solution {
public:
ListNode* rotateRight(ListNode* head, int k) {
if(!head) return NULL;
ListNode *fast,*slow ;
fast = head;
slow = head;
int n = 0;
ListNode *num = head;
while(num){
num = num->next;
n++;
}
k = k % n;
if(!k) return head; //如果为0,不需要旋转
while(k--){
fast = fast -> next;
}
while(fast->next){
slow = slow ->next;
fast = fast ->next;
}
ListNode *phead = slow ->next;
slow ->next = NULL;
fast->next = head;
return phead;
}
};
Leetcode24. 两两交换链表中的节点
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if(!head) return NULL;
ListNode* fakehead = new ListNode(-1);
fakehead->next = head;
ListNode *p,*q;
ListNode *a = fakehead;
while(a->next&&a->next->next){//如果后面没有两个元素了,不进行处理
p = a->next;
q = p->next;
a->next = q;
p->next = q->next;
q->next = p;
a = p;
}
return fakehead->next;
}
};
Leetcode206. 反转链表
class Solution {
public:
ListNode* reverseList(ListNode* head) {
//借助两个指针反转链表
if(!head) return NULL;
ListNode *p,*q,*pre;
p = head;
q = head->next;
p->next = NULL;
while(q){
pre = q->next;
q->next = p;
p = q;
q = pre;
}
return p;
}
};
Leetcode92. 反转链表 II
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
if(!head) return NULL;
ListNode *fakehead = new ListNode(-1);
fakehead->next = head;
ListNode *a,*q,*p,*b,*r,*pre,*g;
a = fakehead;
int j = m-1;
while(j--){
a = a->next;
}
p = a->next ;
g = a->next;
q = a->next;
int k = n-m;
while(k--){
p = p->next;
}
b = p->next;
r = q->next;
//反转q-p之间的链表
while(r!=b){
pre = r->next;
r->next = q;
q = r;
r = pre;
}
a->next = p;
g->next = b;
return fakehead->next;
}
};
Leetcode160. 相交链表
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
//拿到相交位置
ListNode *p=headA;
ListNode *q=headB;
while(p!=q){
if(q) q = q->next;
else q = headA;
if(p) p = p->next;
else p = headB;
}
return p;
}
};
Leetcode142. 环形链表 II
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
if(!head) return NULL;
ListNode *slow = head;
ListNode *fast = head;
while(true){
slow = slow ->next;
if(fast) fast = fast->next;
else return 0;
if(fast) fast = fast->next;
else return 0;
if(fast == slow){
slow = head;
while(slow != fast){
slow = slow -> next;
fast = fast -> next;
}
return slow;
}
}
return 0;
}
};