【牛客网刷题】(第二弹)中等难度题型来了.这些题你都会做吗?

本文介绍了如何创建二叉树、链表节点的k个一组翻转、链表相加以及单链表排序。通过实例代码详细展示了这些操作的实现,包括中序遍历二叉树、链表翻转、链表逆序相加以及链表排序。此外,推荐使用牛客网进行刷题提升技能。
摘要由CSDN通过智能技术生成

🧸🧸🧸各位巨佬大家好,我是猪皮兄弟🧸🧸🧸
在这里插入图片描述


废话不多说,直接来做题!!

一、📖创建二叉树

nowcoder创建二叉树与二叉树的遍历

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef char BTDataType;
typedef struct TreeNode
{
   BTDataType data;
    struct TreeNode*left;
    struct TreeNode*right;
}BT;

BT*CreateNode(BTDataType x)
{
    BT*newnode=(BT*)malloc(sizeof(BT));
    newnode->data=x;
    newnode->left=NULL;
    newnode->right=NULL;
    return newnode;
}

BT*CreateTree(char* str,int *pi)
{
    if(str[*pi]=='#')
    {
      (*pi)++;
      return NULL;
    }
    BT*root=CreateNode(str[(*pi)++]);
    root->left=CreateTree(str,pi);
    root->right=CreateTree(str,pi);
    return root;
}

void InOrder(BT*root)
{
    if(root==NULL)
        return ;
    InOrder(root->left);
    printf("%c ",root->data);
    InOrder(root->right);
}

int main()
{
    char str[100];
    scanf("%s",str);
    int i=0;
    BT*root=CreateTree(str,&i);
    InOrder(root);
    return 0;
}

二、📖链表中的节点每k个一组翻转(面试题)

nowcoder链表中的节点每k个一组翻转
就是先判断有多少组len/k,然后进行组次循环,每次将这一组进行翻转

class Solution {
public:

    ListNode* reverseKGroup(ListNode* head, int k) {
        if(head==nullptr||head->next==nullptr||k<2)
            return head;
        ListNode* newhead=new ListNode(0);
         newhead->next=head;
        ListNode*prev=newhead,*next;
        ListNode*cur=head;
        int len=0;
        while(cur)
        {
            cur=cur->next;
            len++;
        }
        cur=head;
        for(int i=0;i<len/k;i++)
        {
            for(int j=1;j<k;j++)
            {
                next=cur->next;
                cur->next=next->next;
                next->next=prev->next;
                prev->next=next;
            }
            prev=cur;
            cur=cur->next;
        }
        head=newhead->next;
        delete newhead;
        return head;
            
    }
};

三、📖链表相加(中等)

nowcoder链表相加
写一个链表逆置的函数,然后进行简单的链表相加再逆置

class Solution {
public:
    /**
     * 
     * @param head1 ListNode类 
     * @param head2 ListNode类 
     * @return ListNode类
     */
    ListNode* reverse(ListNode*head)
    {
        ListNode*newhead=new ListNode(0);
        newhead->next=head;
        ListNode*n1,*n2,*n3;
        n1=newhead;
        n2=head;
        n3=head->next;
        while(n2)
        {
            n2->next=n1;
            n1=n2;
            n2=n3;
            if(n2)
                n3=n3->next;
        }
        head->next=nullptr;
        delete newhead;
        head=n1;
        return head;
    }
    ListNode* addInList(ListNode* head1, ListNode* head2) {
        // write code here
        if(!head1) return head2;
        if(!head2) return head1;
        head1=reverse(head1);
        head2=reverse(head2);
        ListNode*newhead=new ListNode(0);
        ListNode*tail=newhead;
        int tmp=0;
        while(head1||head2||tmp)
        {
            int val=0;
            if(head1)
            {
                val+=head1->val;
                head1=head1->next;
            }
            if(head2)
            {
                val+=head2->val;
                head2=head2->next;
            }
            ListNode*node=new ListNode((val+tmp)%10);  
            tmp=(val+tmp)/10;
             
            tail->next=node;
            tail=node;
        }
        ListNode*head=reverse(newhead->next);
        
        delete newhead;
        return head;
            
        
        
    }
};

三、📖单链表的排序(中等)时间复杂度O(NlogN)

nowcoder单链表的排序
要求时间复杂度O(NlogN),就先创建一个数组来存储,然后利用algorithm自带的sort函数(用的是快排时间复杂度O(NlogN) )来进行排序,再放回原链表中,空间复杂度O(N),还可以利用冒泡排序的思想,时间复杂度O(N^2),空间复杂度O(1)

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */

class Solution {
public:
    /**
     * 
     * @param head ListNode类 the head node
     * @return ListNode类
     */
    ListNode* sortInList(ListNode* head) {
        // write code here
        vector<int> v;
        int len=0;
        ListNode*cur=head;
        cur=head;
        while(cur)
        {
            v.push_back(cur->val);
            cur=cur->next;
        }
        sort(v.begin(),v.end());
        cur=head;
        int start=0;
        while(cur)
        {
            cur->val=v[start++];
            cur=cur->next;
        }
            
        return head;
    }
};

五 、📖牛客oj总结

牛客网是个很不错的刷题软件,也希望大家能天天在上面刷刷题,大厂offer指日可待啊兄弟们。刷起来。

  • 14
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 15
    评论
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猪皮兄弟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值