链表的刷题-leetcode

1

 反转链表LCR 024. 反转链表 - 力扣(LeetCode)

struct ListNode* reverseList(struct ListNode* head)
{

    if (head == NULL)
    {
        return NULL;
    }
    struct ListNode* n1 = NULL;//先将n1置为空置,不能置为任意节点
    struct ListNode* n2 = head;
    struct ListNode* n3 = n2->next;
    while (n2)
    {
        n2->next = n1;
        n1 = n2;
        n2 = n3;
        if (n3)
            n3 = n3->next;
    }
    return n1;
}

既然称反转链表,意思是指将链表的箭头方向指向前面的链表节点。将原先的头节点变成尾节点,然后依次反转。那么完成此题仅仅需要三个指针,用一个指针1先指向空值,然后指针2指向原先的头节点,指针3的作用指向原先链表的第二个节点(即头节点的下一个节点),指针2指向了指针1,指针1是指针2的前一个节点,然后指针2不能用next指针指向下一个节点,这时候用指针3来更新指针2的位置,指针1也更新到指针2原先的位置,以此往复,等到指针2的走到空置,这时的节点已经全部反转完成。

2

 LCR 077. 排序链表 - 力扣(LeetCode)

int compare(const void* a, const void* b) {
    return (*(int*)a - *(int*)b);
}
struct ListNode* sortList(struct ListNode* head)
{
    Node* cur = head;
    int i = 0;
    while (cur)
    {
        cur = cur->next;
        i++;
    }
    int* arr = (int*)malloc(i * sizeof(int));
    cur = head;
    i = 0;
    while (cur)
    {
        *(arr + i) = cur->val;
        i++;
        cur = cur->next;
    }
    qsort(arr, i, sizeof(int), compare);
    cur = head;
    i = 0;
    while (cur)
    {
        cur->val = *(arr + i);
        i++;
        cur = cur->next;
    }
    free(arr);
    return head;
}

 这道题的大致思路就是将链表的各个节点值依次遍历放进数组中,然后用C语言库中的qsort()快排排好,再依次放进原先的链表中。排序就完成了。 

 3

2. 两数相加 - 力扣(LeetCode)

typedef struct ListNode listNode;
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
    int num = 0;
    listNode* sum1 = l1;
    listNode* sum2 = l2;

    int copy1 = 0;//用于sum1和sum2的val值存储,以用于赋值
    int copy2 = 0;

    listNode* new = (listNode*)malloc(sizeof(listNode));//创建新的节点,并初始化
    new->val = 0;
    new->next = NULL;

    listNode* ret = new;//用于函数返回

    listNode* ptail = NULL;//用于记录new 节点的最后一个有效节点
    while (sum1 || sum2)
    {
        if (sum1 == NULL)
            copy1 = 0;
        else
            copy1 = sum1->val;
        if (sum2 == NULL)
            copy2 = 0;
        else
            copy2 = sum2->val;

        int tmp = (copy2 + copy1 + num);
        num = tmp >= 10 ? 1 : 0;
        new->val = tmp % 10;
        if ((sum1 && sum1->next) || (sum2 && sum2->next))
        {

            listNode* tail = (listNode*)malloc(sizeof(listNode));
            if (tail == NULL)
            {
                perror("tail :: malloc");
                return NULL;
            }

            new->next = tail;
            tail->next = NULL;
        }
        ptail = new;//这里来记录返回链表的最后一个有效节点,用于判断链表最后一个节点的val值是否为大于10的值
        new = new->next;
        if (sum1)
            sum1 = sum1->next;
        if (sum2)
            sum2 = sum2->next;


        if (num == 1 && sum1 == NULL && sum2 == NULL)
        {
            listNode* tail = (listNode*)malloc(sizeof(listNode));
            if (tail == NULL)
            {
                perror("tail :: malloc");
                return NULL;
            }
            ptail->next = tail;
            tail->next = NULL;
            tail->val = 1;
        }
    }
    return ret;
}

这道题我解的比较暴力,首先要明确目标链表生成节点的几个条件:两个条件链表没有走向空值就、如果说两个节点的值相加再加上进位数大于10(因为链表中的值只能存个位数)、当进位数为1而两个链表均走到空值时。

4

138. 随机链表的复制 - 力扣(LeetCode)

这道题需要再原先的链表上进行操作:在每一个原节点后面出创建一个新节点,每个新节点的节点指针指向了前一个节点指向的节点的后一个节点,创建成功后,就将节点分离出来。结果就复制成功了。

struct Node* copyRandomList(struct Node* head)
{
    if (head == NULL)
    {
        return NULL;
    }
    struct Node* pcur = head;
    while (pcur)
    {
        struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
        newnode->next = pcur->next;
        newnode->random = NULL;
        pcur->next = newnode;
        newnode->val = pcur->val;
        pcur = newnode->next;
    }
    //进行random
    pcur = head;
    while (pcur)
    {
        if (pcur->random == NULL)
        {
            pcur->next->random = NULL;
        }
        else
        {
            pcur->next->random = pcur->random->next;
        }
        pcur = pcur->next->next;
    }
    pcur = head;
    struct Node* newhead = (struct Node*)malloc(sizeof(struct Node));
    struct Node* newhead1 = newhead;
    while (pcur)
    {
        newhead->next = pcur->next;
        pcur->next = pcur->next->next;
        newhead = newhead->next;
        pcur = pcur->next;
    }
    return newhead1->next;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值