链表,nullptr,三目运算符

本文介绍了如何在C++中使用链表进行两数相加、合并两个有序链表以及复制带有随机指针的链表的解决方案,展示了链表的基本操作和LeetCode上的相关题目实例。
摘要由CSDN通过智能技术生成

链表是一种数据结构,其中每个元素都可以指向下一个元素,根据这一特性,我们可以从头指针依次遍历整个链表,链表遍历慢,但删改快

nullptr在c++中充当空指针,而NULL充当0

三目运算符格式

int a,b,c;
c = a > b ? a : b;//问号前为真则返回冒号前,否则返回冒号后的

下面是三道力扣上的链表题

两数相加
https://leetcode.cn/problems/add-two-numbers/description/?envType=study-plan-v2&envId=top-interview-150

/**
 * 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* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode *head = nullptr, *tail = nullptr;
        int carry = 0;
        while(l1 || l2)
        {
            int n1 = l1 ? l1->val : 0;
            int n2 = l2 ? l2->val : 0;
            int sum = n1 + n2 + carry;
            if(!head)
            {
                head = tail = new ListNode(sum % 10);
            }
            else
            {
                tail->next = new ListNode(sum % 10);
                tail = tail->next;
            }
            carry = sum / 10;
            if(l1)
            {
                l1 = l1->next;
            }
            if(l2)
            {
                l2 = l2->next;
            }
        }
        if(carry)
        {
            tail->next = new ListNode(carry);
        }
        return head;
    }
};

合并两个有序链表
https://leetcode.cn/problems/merge-two-sorted-lists/description/?envType=study-plan-v2&envId=top-interview-150

/**
 * 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* mergeTwoLists(ListNode* list1, ListNode* list2) {
        ListNode *head = nullptr,*tail = nullptr;
        if(!list1)
        {
            return list2;
        }
        if(!list2)
        {
            return list1;
        }
        while(list1 && list2)
        {
            int n1 = list1 ? list1->val : 200;
            int n2 = list2 ? list2->val : 200;
            int temp;
            if(n1 < n2)
            {
                list1 = list1->next;
                temp = n1;
            }
            else
            {
                list2 = list2->next;
                temp = n2;
            }
            if(!head)
            {
                head = tail = new ListNode(temp);
            }
            else
            {
                tail->next = new ListNode(temp);
                tail = tail->next;
            }
        }
        if(list1)
        {
            tail->next = list1;
        }
        else
        {
            tail->next = list2;
        }
        return head;
    }
};

随机链表的复制
https://leetcode.cn/problems/copy-list-with-random-pointer/description/?envType=study-plan-v2&envId=top-interview-150

/*
// 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 head;
        }
        Node *nhead = nullptr,*tail = nullptr,*temp = head;
        unordered_map <Node*,Node*> map;
        temp = head;
        nhead = tail = new Node(temp->val);
        map[head] = tail;
        temp = temp->next; 
        while(temp)
        {
            tail->next = new Node(temp->val);
            map[temp] = tail->next;
            tail = tail->next;
            temp = temp->next;
        }
        temp = head;
        while(temp)
        {
            if(temp->random)
            {
                map[temp]->random = map[temp->random];
            }
            temp = temp->next;
        }
        return nhead;
        
    }
};
  • 13
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值