题解 | #牛群旋转#

牛群旋转

https://www.nowcoder.com/practice/5137e606573843e5bf4d8ea0d8ade7f4

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param head ListNode类 
 * @param k int整型 
 * @return ListNode类
 */
struct ListNode* rotateLeft(struct ListNode* head, int k ) {
    // write code here
    struct ListNode *cur=head;
    int  length=0;
    while(cur){
        ++length;
        cur=cur->next;
    }
    k%=length;
    struct ListNode*fast=head,*slow=head;
    while(k--){
        fast=fast->next;
    }
    while(fast->next){
        fast=fast->next;
        slow=slow->next;
    }
    fast->next=head;
    head=slow->next;
    slow->next=NULL;

    return head;

}

全部评论

相关推荐

10-13 16:58
门头沟学院 Java
面了100年面试不知...:一周七天,一天去一家上班😍😍😍
点赞 评论 收藏
分享
牛客54175811...:今年对双非很难。1、争取一段大厂实习经历,2、狂磕八股,3、再跑个难度提升的项目。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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