题解 | #链表内指定区间反转#

链表内指定区间反转

https://www.nowcoder.com/practice/b58434e200a648c589ca2063f1faf58c

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param head ListNode类 
 * @param m int整型 
 * @param n int整型 
 * @return ListNode类
 */
struct ListNode* reverseBetween(struct ListNode* head, int m, int n ) {
    // write code here
    int i;
    struct ListNode* phead=NULL;
    struct ListNode* pphead=NULL;
     struct ListNode* pre=NULL;
      struct ListNode* next=NULL;
      struct ListNode* store=NULL;
        struct ListNode* current=NULL;
         struct ListNode* pcurrent=NULL;
    if(m==n){
        return head;
    }
    else{
        pphead=head;
    phead=head;          //储存头节点
        for(i=0;i<m-1;i++){
    current=head;
    head=head->next;    //使head指向下一个节点
}                       //使head指向m节点位置
    for(i=0;i<n-1;i++)  
    {
        phead=phead->next;
    }                       //使phead指向n的位置
        pre=head;
        head=head->next;
        next=head->next;
        if((m==1)&&(n==2)) //m,n在前两位节点,我写糊涂了,只能这么改了
        {
            head->next=pre;
            pre->next=next;
            return head;
        }
        if(head==phead) //m,n相差为1
        {
            head->next=pre;
            pre->next=next;
            current->next=head;
            head=pphead;
            return head;
        }
        else{
 while(next!=phead){
            head->next=pre;
            pre=head;
            head=next;
            next=next->next;
        }
        while(m==1){
            (pphead->next)=(phead->next);
            head->next=pre;
            next->next=head;
             
            phead->next=head;
           
            return phead;
        }   
      
head->next=pre;
    store=next->next;           //储存n的下一个节点
    next->next=head;
     pcurrent=current->next;
    current->next=phead;
    pcurrent->next=store;
    head=pphead;
    return head;
        }
       
    }
}

全部评论

相关推荐

HR_丸山彩同学:你的项目描述里,系统设计讲了很多:MemCube是什么、三级存储架构怎么设计、四种遗忘策略分别是什么。这些面试的时候讲没问题,但简历上不需要这么细。 简历要突出的是影响力,不是实现细节。面试官看简历的时候想知道的是「这个项目有多大价值」,不是「这个项目具体怎么实现的」。实现细节是面试时候聊的 怎么改:技术细节可以精简为一句「采用三级存储架构+四种遗忘策略」,把省出来的篇幅用来写影响力。比如:项目有没有开源?有没有写成技术博客?有没有被别人使用过? 校园经历没有任何信息量,任何人都可以写这句话,写了等于没写。更关键的是,你投的是技术岗,校园活动经历本来就不是加分项。如果非要写,必须写出具体的数字和成果。如果你没有这些数字,那就老老实实删掉 「端到端耗时缩减30-40%」要给出确切数字和绝对值。从1000ms降到600ms是降了40%,从100ms降到60ms也是降了40%,但这两个含义完全不一样。其他也是,涉及到数据,准备好证据,口径统一,面试会问 「熟练」「熟悉」「了解」混在一起用,读起来很乱。而且「了解前端需求」最好改成「具备前后端协作经验」
点赞 评论 收藏
分享
开发转测第二人:没实习的话,两个项目吧,八股也要准备一下,这个时间点有点小晚了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务