leetcode链表题目整理

3 篇文章 0 订阅
2 篇文章 0 订阅

剑指Offer

剑指Offer 06.从尾到头打印链表

在这里插入图片描述

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> reversePrint(ListNode* head) {
        int top = -1;
        int st[10010];
        ListNode* p = head;
        while(p!=NULL)
        {
            st[++top] = p->val;
            p = p->next;
        }
        vector<int> ans(top+1);
        int k = 0;
        while(top!=-1)
            ans[k++] = st[top--];
        return ans;
    }
};

递归

class Solution {
public:
    int k=0;
    vector<int> res;
    void getLength(ListNode* head)
    {
        int cnt = 0;
        ListNode* p = head;
        while(p!=NULL)
        {
            cnt++;
            p = p->next;
        }
        res.resize(cnt);
    }
    void reverseP(ListNode* head)
    {
        if(!head)
            return;
        reverseP(head->next);
        res[k++] = (head->val);
    }
    vector<int> reversePrint(ListNode* head) {
        getLength(head);
        reverseP(head);
        return res;
    }
};

剑指Offer 18.删除链表的节点

在这里插入图片描述

代码

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* deleteNode(ListNode* head, int val) {
        ListNode* lfirst = (ListNode*)malloc(sizeof(ListNode));
        lfirst->next = head;
        ListNode* p = lfirst;
        while(p&&p->next!=NULL)
        {
            if(p->next->val==val)
            {
                p->next = p->next->next;
            }
            p = p->next;
        }
        return lfirst->next;
    }
};

剑指Offer 22.链表中倒数第k个节点

在这里插入图片描述

双指针

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* getKthFromEnd(struct ListNode* head, int k){
    struct ListNode* p = head;
    struct ListNode* q = p;
    
    while(q&&k--)
    {
        q = q->next;
    }
    if(k>0)
        return NULL;
    while(q!=NULL)
    {
        p = p->next;
        q = q->next;
    }
    return p;
}

剑指Offer 24.反转链表

在这里插入图片描述

头插法

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* lst = new ListNode(0,NULL);
        ListNode* p = head;
        ListNode* q;
        while(p!=NULL)
        {
            q = p->next;
            p->next = lst->next;
            lst->next = p;
            p = q;
        }
        return lst->next;
    }
};

剑指offer 35. 复杂链表的复制

在这里插入图片描述

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* deleteNode(ListNode* head, int val) {
        ListNode* lfirst = (ListNode*)malloc(sizeof(ListNode));
        lfirst->next = head;
        ListNode* p = lfirst;
        while(p&&p->next!=NULL)
        {
            if(p->next->val==val)
            {
                p->next = p->next->next;
            }
            p = p->next;
        }
        return lfirst->next;
    }
};

hashmap

/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* next;
    Node* random;
    
    Node(int _val) {
        val = _val;
        next = NULL;
        random = NULL;
    }
};
*/
class Solution {
public:
    Node* copyRandomList(Node* head) {
        if(!head)
            return NULL;
        unordered_map<Node*,Node*> match;
        Node * newhead = new Node(head->val);
        Node* copy = newhead;
        Node* p = head;
        while(p!=NULL)
        {
            match[p] = copy;
            p = p->next;
            //复制next
            if(p==NULL)
            {
                copy->next = NULL;
                match[p] = NULL;
            }
            else copy->next = new Node(p->val);
            copy = copy->next;
        }
        p = head;
        copy = newhead;
        //复制random
        while(p!=NULL)
        {
            copy->random = match[p->random];
            p = p->next;
            copy = copy->next;
        }
        return newhead;
    }
};

原地复制

/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* next;
    Node* random;
    
    Node(int _val) {
        val = _val;
        next = NULL;
        random = NULL;
    }
};
*/
class Solution {
public:
    Node* copyRandomList(Node* head) {
        if(!head)
            return NULL;
        Node* p = head;
        Node* copy = NULL;
        while(p!=NULL)
        {
            copy = new Node(p->val);
            copy->next = p->next;
            p->next = copy;
            p = p->next->next;
        }
        p = head;
        while(p!=NULL)
        {
            if(p->random!=NULL)
            {
                p->next->random = p->random->next;
            }
            p = p->next->next;
        }
       
        Node* newhead = head->next;
        p = head;
        while(p&&p->next)
        {
            Node* temp = p->next;
            p->next = temp->next;
            p = temp;
        }
        return newhead;
    }
};

剑指Offer 52.两个链表的第一个公共节点

在这里插入图片描述

双指针

/**
 * 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* node1 = headA, *node2 = headB;
        while(node1!=node2)
        {
            node1 = node1!=NULL? node1->next : headB;
            node2 = node2!=NULL? node2->next : headA;
        }
        return node1;
    }
};
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Leetcode 高频考题整理确实是很有帮助的,以下是一些常见的 Leetcode 高频考题整理: 1. 数组和字符串问题: - 两数之和 (Two Sum) - 三数之和 (Three Sum) - 最长回文子串 (Longest Palindromic Substring) - 盛最多水的容器 (Container With Most Water) - 下一个排列 (Next Permutation) 2. 链表问题: - 反转链表 (Reverse Linked List) - 删除链表中的倒数第N个节点 (Remove Nth Node From End of List) - 合并两个有序链表 (Merge Two Sorted Lists) - 链表中环的检测 (Linked List Cycle) - 环形链表的起始点 (Linked List Cycle II) 3. 树和图问题: - 二叉树的遍历 (Binary Tree Traversal) - 二叉树的最大深度 (Maximum Depth of Binary Tree) - 二叉树的最小深度 (Minimum Depth of Binary Tree) - 图的深度优先搜索 (Depth First Search) - 图的广度优先搜索 (Breadth First Search) 4. 动态规划问题: - 爬楼梯 (Climbing Stairs) - 最大子序和 (Maximum Subarray) - 打家劫舍 (House Robber) - 不同路径 (Unique Paths) - 最长递增子序列 (Longest Increasing Subsequence) 5. 排序和搜索问题: - 快速排序 (Quick Sort) - 归并排序 (Merge Sort) - 二分查找 (Binary Search) - 搜索旋转排序数组 (Search in Rotated Sorted Array) - 寻找峰值 (Find Peak Element) 这只是一些常见的 Leetcode 高频考题整理,还有很多其他题目也值得关注。通过刷题和整理高频题目,可以提高对算法和数据结构的理解和应用能力。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值