目录
203.移除链表元素
707.设计链表
206.反转链表
24. 两两交换链表中的节点
19.删除链表的倒数第N个节点
面试题 02.07. 链表相交
142.环形链表II
203.移除链表元素
203. 移除链表元素 - 力扣(LeetCode)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode *dummyhead = new ListNode(0,head);
ListNode *pre = dummyhead;
ListNode *cur = head;
while (cur!= nullptr)
{
if (cur->val==val)
{
pre->next = cur->next;
delete cur;
cur = pre->next;
}
else
{
cur = cur->next;
pre = pre->next;
}
}
return dummyhead->next;
}
};
707.设计链表
707. 设计链表 - 力扣(LeetCode)
class MyLinkedList {
private:
struct ListNode{
int val;
ListNode* next;
ListNode():val(0),next(nullptr){}
ListNode(int v):val(v),next(nullptr){}
ListNode(int v, ListNode* n):val(v),next(n){}
};
int tailindex;
ListNode* dummyhead;
public:
MyLinkedList() {
tailindex = -1;
dummyhead = new ListNode();
}
int get(int index) {
if(index<0 || index>tailindex)return -1;
ListNode *cur = dummyhead->next;
while(index){
cur = cur->next;
index--;
}
return cur->val;
}
void addAtHead(int val) {
ListNode* cur =new ListNode(val,dummyhead->next);
dummyhead->next = cur;
tailindex++;
}
void addAtTail(int val) {
ListNode* cur =new ListNode(val);
ListNode *pre = dummyhead;
while (pre->next){
pre = pre->next;
}
pre->next = cur;
tailindex++;
}
void addAtIndex(int index, int val) {
if(index<0 || index>tailindex+1)return;
if (index==0)addAtHead(val);
else if(index==tailindex+1)addAtTail(val);
else{
ListNode *cur = dummyhead;
while(index!=0){
cur = cur->next;
index--;
}
ListNode *n = new ListNode(val,cur->next);
cur->next = n;
tailindex++;
}
}
void deleteAtIndex(int index) {
if(index<0 || index>tailindex) return;
ListNode*cur = dummyhead;
while (index!=0){
cur = cur->next;
index--;
}
ListNode *tmp = cur->next;
cur->next = cur->next->next;
delete tmp;
tailindex--;
}
};
/**
* Your MyLinkedList object will be instantiated and called as such:
* MyLinkedList* obj = new MyLinkedList();
* int param_1 = obj->get(index);
* obj->addAtHead(val);
* obj->addAtTail(val);
* obj->addAtIndex(index,val);
* obj->deleteAtIndex(index);
*/
206.反转链表
206. 反转链表 - 力扣(LeetCode)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if (head==nullptr || head->next==nullptr)return head;
ListNode *pre = nullptr,*cur = head,*tmp = cur->next;
while(tmp){
cur->next = pre;
pre = cur;
cur = tmp;
tmp = tmp->next;
}
cur->next = pre;
return cur;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if (head ==nullptr || head->next==nullptr)return head;
ListNode *tmp = reverseList(head->next);
head->next->next = head;
head->next = nullptr;
return tmp;
}
};
24. 两两交换链表中的节点
24. 两两交换链表中的节点 - 力扣(LeetCode)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if (head ==nullptr || head->next==nullptr)return head;
ListNode*dummyhead = new ListNode(0,head);
ListNode *pre = dummyhead,*cur = dummyhead->next;
while(cur!=nullptr && cur->next!=nullptr){
ListNode*tmp = cur->next->next;
pre->next = cur->next;
pre->next->next = cur;
cur->next = tmp;
pre = cur;
cur = tmp;
}
return dummyhead->next;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if (head ==nullptr || head->next==nullptr)return head;
ListNode* tmp =head->next;
head->next = swapPairs(tmp->next);
tmp->next= head;
return tmp;
}
};
19.删除链表的倒数第N个节点
19. 删除链表的倒数第 N 个结点 - 力扣(LeetCode)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode *dummyhead = new ListNode(0,head);
ListNode *fast = dummyhead,*slow = dummyhead;
while(n!=-1){
fast = fast->next;
n--;
}
while (fast!=nullptr){
fast =fast->next;
slow = slow->next;
}
ListNode*tmp = slow->next;
slow->next = slow->next->next;
delete tmp;
return dummyhead->next;
}
};
面试题 02.07. 链表相交
160. 相交链表 - 力扣(LeetCode)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode *p1 = headA,*p2 = headB;
while(p1!=p2){
if(p1!=nullptr){
p1 = p1->next;
}else p1 = headB;
if (p2!=nullptr){
p2 = p2->next;
}else p2 = headA;
}
return p1;
}
};
142.环形链表II
142. 环形链表 II - 力扣(LeetCode)
/**
* 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,*slow = head;
while (fast!=nullptr && fast->next!=nullptr){
fast = fast->next->next;
slow = slow->next;
if(fast ==slow){
fast = head;
while (fast!=slow){
fast = fast->next;
slow = slow->next;
}
return fast;
}
}
return nullptr;
}
};