力扣刷题(代码随想录顺序)
链表
题目挺简单的没什么好说的我只要说一句就跳过前面的题目了
!虚拟头真的很好用!
203.移除链表元素
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode* p = head;
if(head==NULL) return head;
// p->next!=NULL 和p!=Null和 什么时候p=p->next是很大关系的 要注意
while(p!=NULL){
if( (p->next)!=NULL&&(p->next)->val == val ) {
p->next = (p->next)->next;
}
else{
p=p->next;
}
}
if(head->val == val){
head = head->next;
}
return head;
}
}
707.设计链表
class MyLinkedList {
public:
struct ListNode{
int val;
ListNode* next;
ListNode(int x):val(x),next(NULL){}
ListNode(int x,ListNode* p):val(x),next(p){}
};
ListNode* head = new ListNode(0);
int size=-1;
int get(int index) {
if(index<0||index>size) return -1;
ListNode* p = head;
while(index--) p=p->next;
return p->next->val;
}
void addAtHead(int val) {
ListNode* p = new ListNode(val);
p->next = head->next;
head->next = p;
size++;
}
void addAtTail(int val) {
ListNode* p= head;
while(p->next!=NULL) p=p->next;
p->next = new ListNode(val);
size++;
}
void addAtIndex(int index, int val) {
int tsize=size+1;
if(index <0) {
addAtHead(val);
return;
}
else if(index>tsize) return;
ListNode* p = head;
while(index--){
p=p->next;
}
ListNode* temp = new ListNode(val);
temp->next=p->next;
p->next=temp;
size++;
}
void deleteAtIndex(int index) {
ListNode* tmp = head;
if(index<0||index>size) return;
ListNode* p = head;
while(index--) p=p->next;
p->next = p->next->next;
size--;
}
};
206.反转链表
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* pre;
pre = nullptr;
ListNode* cur = head;
while(cur){
ListNode* temp = cur->next;
cur->next = pre;
pre = cur;
cur = temp;
}
head = pre;
return head;
}
};
19.删除链表的倒数第N个节点
经典双指针法 当fast走到末尾的时候 slow只要比fast慢N步,那么slow就是倒数第N个节点
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* t = new ListNode(0,head);
head = t;
ListNode* fastp = head;
ListNode* slowp = head;
int fast = 0;
int slow = 0;
ListNode* temp=NULL;
while(true){
if(fastp->next == NULL){
slowp->next = slowp->next->next;
return head->next;
}
if (fast - slow==n){
slow++;
fast++;
fastp = fastp->next;
slowp = slowp->next;
}
else {
fast++;
fastp = fastp->next;
}
}
return head->next;
}
};
面试题 02.07. 链表相交
说实话看老半天没看懂题目,后面看题懂了,相交的意思是不仅val一样,甚至于地址都一样
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode* a = headA;
ListNode* b = headB;
while (a != b)
{
a = a != nullptr ? a->next : headB;
b = b != nullptr ? b->next : headA;
}
return a;
}
};
这道题挺有意思的,可以瞧瞧
142.环形链表II
首先给两点提示就是
兔子和小乌龟在同一条环形跑道上,兔子速度是小乌龟的两倍,而兔子总是晚小乌龟一点点或者等于小乌龟的时间出发,那么这不就说明一定会在半圈的地方或者半圈后面一些追上小乌龟吗?
也就是小乌龟顶多跑一圈就被小兔子套圈了 ,n=1
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode* ptr=head;
ListNode* fast = head;
ListNode* slow = head;
int flag = 0,step = 0;
if(head==NULL) return NULL;
while(true){
if(fast->next==nullptr) return NULL;
else fast = fast->next;
if(fast->next==nullptr) return NULL;
else fast = fast->next;
slow = slow->next;
if (flag == 1){
ptr=ptr->next;
step++;
}
if (fast==slow) flag=1;
if (ptr==slow&& flag==1) return ptr;
}
}
};