链表专题
203. 移除链表元素
思路: 1)删除头部相同的val 2)来到第一个不是val的位置
/**
* 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) {
// delete head val
while(head && head->val == val) {
head = head->next;
}
// 来到第一个不是val的位置
auto cur = head;
while(cur) {
if(cur->next && cur->next->val == val) {
cur->next = cur->next->next;
} else {
cur = cur->next;
}
}
return head;
}
};
206. 反转链表
递归 和 迭代 实现
画图
递归
class Solution {
public:
ListNode* dfs(ListNode* pre, ListNode* cur) {
if(cur == nullptr) return pre;
auto temp = cur->next;
cur->next = pre;
return dfs(cur,temp);
}
ListNode* reverseList(ListNode* head) {
return dfs(nullptr,head);
}
};
迭代
/**
* 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) {
ListNode* pre = nullptr;
ListNode* cur = head;
while(cur) {
auto next = cur->next; // 抓一下后面的节点
cur->next = pre; // 当前当前节点指向nullptr
pre = cur; // pre 来到 cur位置
cur = next; // cur 来到next位置
}
return pre;
}
};
707. 设计链表
class MyLinkedList {
public:
struct Node {
int val;
Node* next;
Node(int _val) : val(_val),next(nullptr) {}
}*head;
MyLinkedList() {
head = nullptr;
}
int get(int index) {
if(index < 0) return -1;
else { // index > 0 // index > i return null
int i = 0;
auto p = head;
while(p && i < index) {
i++; p = p->next;
}
return p == nullptr ? -1 : p->val;
}
}
void addAtHead(int val) {
auto cur= new Node(val);
cur->next = head;
head = cur;
}
void addAtTail(int val) {
if(head == nullptr) addAtHead(val);
else {
auto p = head;
while(p->next) p = p->next;
p->next = new Node(val);
}
}
void addAtIndex(int index, int val) {
/*
* 在index位置添加val
* 1) 如果index <= 0 在head insert
* 2) index > 0 计算长度
* 3)index <= len index == len addToTail
index < len 找到index - 2 的位置 insert
*
*/
if(index <= 0) addAtHead(val);
else {
// index > 0
int len = 0;
for(auto p = head; p; p = p->next) len++;
if(index <= len) {
if(index == len) addAtTail(val);
else {
auto p = head;
for(int i = 0; i < index - 1;i ++) p = p->next;
auto cur = new Node(val);
cur->next = p->next;
p->next = cur;
}
}
}
}
void deleteAtIndex(int index) {
/**
* index < 9 无效返回
inex == 0 delete head;
index > 0 找到index - 1位置之前
*/
if(index < 0) return;
else {
if(index == 0) head = head->next;
else {
int len = 0;
for(auto p = head; p; p = p->next) len++;
if(index < len) {
auto p = head;
int i = 0;
while(p && i < index - 1) {
i++;p = p->next;
}
p->next = p->next->next;
}
}
}
}
};
/**
* 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);
*/
24. 两两交换链表中的节点
画图
/**
* 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) {
ListNode* d = new ListNode(-1);
d->next = head;
for(auto p = d; p->next &&p->next->next;) {
auto a = p->next;
auto b = a->next;
p->next = b;
a->next = b->next;
b->next = a;
p = a;
}
return d->next;
}
};
19. 删除链表的倒数第 N 个结点
- fast走n+1步
- slow和fast各走一步
/**
* 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* dummy = new ListNode(-1); dummy->next = head;
auto slow = dummy;
auto fast = dummy;
while(fast && n--) fast = fast->next;
fast = fast->next;
while(fast) {
slow = slow->next;
fast = fast->next;
}
slow->next = slow->next->next;
return dummy->next;
}
};
面试题 02.07. 链表相交
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
auto a = headA;
auto b = headB;
while(a != b) {
a = (a == NULL) ? headB : a->next;
b = (b == NULL) ? headA : b->next;
}
return a == b ? a : NULL;
}
};