class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode* temp = new ListNode(0);
temp->next = head;
ListNode*cur = temp;
while(cur->next != nullptr){
if(cur->next->val == val){
cur->next = cur->next->next;
}else{
cur = cur->next;
}
}
return temp->next;
}
};
class MyLinkedList {
public:
struct LinkedNode{
int val{};
LinkedNode* next{nullptr};
LinkedNode(int inVal): val(inVal){}
};
MyLinkedList() {
m_dummyNode = new LinkedNode(0);
}
int get(int index) {
if(index > (m_size -1) || index < 0) return -1;
LinkedNode* cur = m_dummyNode->next;
while(index--){
cur = cur->next;
}
return cur->val;
}
void addAtHead(int val) {
LinkedNode* node = new LinkedNode(val);
node->next = m_dummyNode->next;
m_dummyNode->next = node;
m_size++;
}
void addAtTail(int val) {
LinkedNode *tail = new LinkedNode(val);
LinkedNode *cur = m_dummyNode;
while(cur->next != nullptr){
cur = cur->next;
}
cur->next = tail;
m_size++;
}
void addAtIndex(int index, int val) {
if(index > m_size) return;
LinkedNode* node = new LinkedNode(val);
LinkedNode* cur = m_dummyNode;
while(index--&& cur != nullptr){
cur = cur->next;
}
node->next = cur->next;
cur->next = node;
m_size++;
}
void deleteAtIndex(int index) {
if(index > m_size-1 || index < 0) return;
LinkedNode* cur = m_dummyNode;
while(index--&& cur != nullptr){
cur = cur->next;
}
LinkedNode* temp= cur->next;
if(cur->next != nullptr)cur->next = cur->next->next;
m_size--;
}
private:
int m_size{};
LinkedNode *m_dummyNode { nullptr };
};
class Solution {
public:
ListNode* reverse(ListNode* pre,ListNode* cur){
if(cur == nullptr) return pre;
ListNode* temp = cur->next;
cur->next = pre;
return reverse(cur,temp);
}
ListNode* reverseList(ListNode* head) {
return reverse(nullptr,head);
}
};
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode* dummyNode = new ListNode(0);
dummyNode->next = head;
ListNode* cur = dummyNode;
while(cur->next!=nullptr && cur->next->next != nullptr){
ListNode* temp = cur->next;
ListNode* temp1 = cur->next->next->next;
cur->next = cur->next->next;
cur->next->next = temp;
temp->next = temp1;
cur = cur->next->next;
}
return dummyNode->next;
}
};
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* dummpyNode = new ListNode(0);
dummpyNode->next = head;
ListNode* slowNode = dummpyNode;
ListNode* fastNode = dummpyNode;
while(n-- && fastNode != nullptr) fastNode = fastNode->next;
fastNode = fastNode->next;
while(fastNode != nullptr){
slowNode = slowNode->next;
fastNode = fastNode->next;
}
slowNode->next = slowNode->next->next;
return dummpyNode->next;
}
};
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
int lenA{},lenB{};
ListNode* curA = headA;
ListNode* curB = headB;
while(curA!=nullptr){
lenA++;
curA = curA->next;
}
while(curB != nullptr){
lenB++;
curB = curB->next;
}
curA = headA;
curB = headB;
if(lenB > lenA){
swap(lenA,lenB);
swap(curA,curB);
}
int delta = lenA - lenB;
while(delta--){
curA = curA->next;
}
while(curA!=nullptr){
if(curB == curA) return curB;
curB = curB->next;
curA = curA->next;
}
return {};
}
};
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
if(!head) return{};
ListNode* slow = head;
ListNode* fast = head;
while(fast->next!=nullptr && fast->next->next!=nullptr){
slow = slow->next;
fast = fast->next->next;
if(slow == fast){
slow = head;
while(slow != fast){
slow = slow->next;
fast = fast->next;
}
return slow;
}
}
return {};
}
};
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(!l1) return l2;
if(!l2) return l1;
ListNode* ans;
if(l1->val <= l2->val){
ans = l1;
l1 = l1->next;
}else{
ans = l2;
l2 = l2->next;
}
ans->next = mergeTwoLists(l1,l2);
return ans;
}
};