ListNode

链表

1.移除链表元素

/**
 * 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(-1);
        dummyhead -> next = head;
        ListNode* cur = dummyhead;
        
        while(cur -> next != nullptr){
            if(cur->next->val == val){
                ListNode* temp = cur->next;
                cur->next = cur->next->next;
                delete temp;
            }else{
                cur = cur->next;
            }
        }
        head = dummyhead->next;
        delete dummyhead;
        return head;
    }
};
//注意链表需要释放被删除的元素
//注意链表数据结构的构造

2.翻转链表

// 1.头插法
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if(head == nullptr) return nullptr;
        ListNode* dummyhead = new ListNode(-1);
        dummyhead -> next = head;
        ListNode* cur = head;
        while(cur -> next != nullptr){
            auto temp  = cur->next->next;
            auto temp1 = dummyhead->next;
            dummyhead->next = cur->next;
            cur->next->next = temp1;
            cur->next = temp;
        }
        return dummyhead->next;
    }
};
// 2.双指针
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* cur = head;
        ListNode* pre = nullptr;
        ListNode* temp = nullptr;
        while(cur){
            temp = cur -> next;
            cur->next = pre;
            pre = cur;
            cur = temp;
        }
        return pre;
    }
};
// 3.递归解法
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if(head == nullptr) return nullptr;
        if(head -> next == nullptr) return head;
        ListNode* temp = reverseList(head->next);
        head -> next ->next = head;
        head -> next = nullptr;
        return temp; 
    }
};

3.两两交换链表中的节点

class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        ListNode* cur = head;
        ListNode* temp = nullptr;
        ListNode* dummyhead = new ListNode(-1);
        dummyhead -> next = head;
        ListNode* pre = dummyhead;
        while(cur != nullptr && cur -> next != nullptr){
            pre -> next = cur -> next;
            temp = cur -> next ->next;
            cur -> next ->next = cur;
            cur -> next = temp;
            pre = cur;
            cur = cur -> next;
        }
        return dummyhead->next;
    }
};

4.删除链表的倒数第N个节点

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* dummyhead = new ListNode(-1);
        dummyhead -> next = head;
        ListNode* pre = dummyhead;
        ListNode* cur = head;
        while(n--){
            if(cur == nullptr) return head;
            cur = cur -> next;        
        }
        while(cur != nullptr){
            cur = cur->next;
            pre = pre->next;
        }
        pre -> next = pre ->next->next;
        return dummyhead->next;
    }
};

5.链表相交

// 1.数学法
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        ListNode *A = headA, *B = headB;
        while (A != B) {
            A = A != nullptr ? A->next : headB;
            B = B != nullptr ? B->next : headA;
        }
        return A;
    }
};
作者:jyd
链接:https://leetcode-cn.com/problems/intersection-of-two-linked-lists-lcci/solution/mian-shi-ti-0207-lian-biao-xiang-jiao-sh-b8hn/
// 2.简单易懂
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        ListNode* curA = headA;
        ListNode* curB = headB;

        int nA = 0;
        int nB = 0;
        while(curA != nullptr){
            curA = curA -> next;
            nA++;
        }
        while(curB != nullptr){
            curB = curB -> next;
            nB++;
        }
        curA = headA;
        curB = headB;

        if(nB > nA){
            swap(curA, curB);
        }
        int diff = abs(nA - nB);

        while(diff--){
            curA = curA -> next;
        }
        while(curA != nullptr){
            if(curA == curB) return curA;
            curA = curA -> next;
            curB = curB -> next;
        }
        return nullptr;
    }
};

6.环形链表 II

class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        ListNode* fastptr = head;
        ListNode* slowptr = head;

        do{
            if(fastptr == nullptr || fastptr->next == nullptr){
                return NULL;
            }
            fastptr = fastptr -> next ->next;
            slowptr = slowptr -> next;
            
        }while(fastptr != slowptr);

        ListNode* cur = head;
        while(cur != fastptr){
            cur = cur -> next;
            fastptr = fastptr -> next;
        }
        return cur;
        
    }
};
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值