NC50 链表中的节点每k个一组翻转(四种语言+代码)

链表中的节点每k个一组翻转

https://www.nowcoder.com/practice/b49c3dc907814e9bbfa8437c251b028e?tpId=188&&tqId=38557&rp=1&ru=/activity/oj&qru=/ta/job-code-high-week/question-ranking

- 题目描述:
图片说明
- 题目链接:
https://www.nowcoder.com/practice/b49c3dc907814e9bbfa8437c251b028e?tpId=188&&tqId=38557&rp=1&ru=/activity/oj&qru=/ta/job-code-high-week/question-ranking

- 设计思想:

-视频讲解链接B站视频讲解
- 复杂度分析:
图片说明
- 代码:
c++版本:

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

class Solution {
public:
    /**
     * 
     * @param head ListNode类 
     * @param k int整型 
     * @return ListNode类
     */

    pair<ListNode*, ListNode*> reverseList(ListNode* head, ListNode* temp) {
        //head代表翻转的头,temp代表尾巴
        ListNode* pre = temp->next;//保存尾巴的下一个节点信息
        ListNode* res = nullptr;//开辟一个新的链表
        ListNode* cur = head;//用来遍历链表head
        while (cur != pre) {
            ListNode* Temp = cur->next;
            cur->next = res;
            res = cur;
            cur = Temp;
        }
        return {temp, head};
    }
    ListNode* reverseKGroup(ListNode* head, int k) {
        // write code here
        if(head == nullptr || head->next == nullptr || k == 1) return head;//特殊情况
        ListNode* res = new ListNode(-1);
        res->next = head;
        ListNode* cur = res;
        while(cur != nullptr){
            ListNode* temp = cur;
            for(int i = 0;i < k;i ++){
                temp = temp->next;
                if(temp == nullptr) return res->next;//如果走到了最后直接返回,不需要后续操作
            }
            ListNode* nxt = temp->next;//因为每一组要反转,需要保存翻转前的下一个节点
            pair<ListNode*, ListNode*>reverse = reverseList(head,temp);
            head = reverse.first;
            temp = reverse.second;
            //把翻转部分的链表重新接到原链表上
            cur->next = head;
            temp->next = nxt;
            cur = temp;
            head = temp->next;
        }
        return res->next;
    }
};

Java版本:

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param head ListNode类 
     * @param k int整型 
     * @return ListNode类
     */
    public ListNode reverseKGroup (ListNode head, int k) {
        // write code here
        if(head==null||head.next==null||k==1) return head;//特殊情况
        ListNode res = new ListNode(-1);
        res.next = head;
        ListNode cur = res;
        while(head != null){
            ListNode temp = cur;
            ///查看剩余部分的长度是否大于K
            for(int i = 0;i < k;i ++){
                temp = temp.next;
                if(temp == null) return res.next;//如果走到了最后直接返回,不需要后续操作
            }
            ListNode nxt = temp.next;///因为每一组要反转,需要保存翻转前的下一个节点
            ListNode []reverse = reverseList(head,temp);
            head = reverse[0];
            temp = reverse[1];
            cur.next = head;
           // head.next = temp;
            temp.next = nxt;
            cur = temp;
            head = temp.next;

        }
        return res.next;
    }
    public ListNode[] reverseList(ListNode head, ListNode temp) {
        ListNode pre = temp.next;
        ListNode res = null;
        ListNode cur = head;
        while(cur != pre){
            ListNode Temp = cur.next;
            cur.next = res;
            res = cur;
            cur =Temp;
       }
        return new ListNode[]{temp, head};
    }
}

Python版本:

# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

#
# 
# @param head ListNode类 
# @param k int整型 
# @return ListNode类
#
class Solution:
    def reverseList(self,head,temp):
        #翻转链表
        pre = temp.next
        res = None
        cur = head
        while cur != pre:
            Temp = cur.next
            cur.next = res
            res = cur
            cur = Temp
        return temp,head

    def reverseKGroup(self , head , k ):
        # write code here
        if head == None or head.next == None or k == 1: return head#特殊情况
        res = ListNode(-1)
        res.next = head
        cur = res
        while head:
            #查看剩余部分的长度是否大于K
            temp = cur
            for i in range(k):
                temp = temp.next
                if temp == None:return res.next#如果走到了最后直接返回,不需要后续操作
            nxt = temp.next#因为每一组要反转,需要保存翻转前的下一个节点
            head,temp = self.reverseList(head,temp)
            #把翻转部分的链表重新接到原链表上
            cur.next = head
            temp.next = nxt
            cur = temp
            head = temp.next
        return res.next;

JavaScript版本:

/*
 * function ListNode(x){
 *   this.val = x;
 *   this.next = null;
 * }
 */

/**
  * 
  * @param head ListNode类 
  * @param k int整型 
  * @return ListNode类
  */
function reverseList( head ,temp) {
        //翻转链表
        let pre = temp.next;
        let res = null;
        let cur = head;
        while(cur != pre){
            let Temp = cur.next;
            cur.next = res;
            res = cur;
            cur =Temp;
       }
        return [temp, head];    
}  
function reverseKGroup( head ,  k ) {
    // write code here
    if(head==null||head.next==null||k==1) return head;//特殊情况
        let res = new ListNode(-1);
        res.next = head;
        let cur = res;
        while(head != null){
            let temp = cur;
            //查看剩余部分的长度是否大于K
            for(let i = 0;i < k;i ++){
                temp = temp.next;
                if(temp == null) return res.next;//如果走到了最后直接返回,不需要后续操作
            }
            let nxt = temp.next;//因为每一组要反转,需要保存翻转前的下一个节点
            [head, temp] = reverseList(head,temp);
            //把翻转部分的链表重新接到原链表上
            cur.next = head;
           // head.next = temp;
            temp.next = nxt;
            cur = temp;
            head = temp.next;

        }
        return res.next;
}
module.exports = {
    reverseKGroup : reverseKGroup
};
牛客题霸 文章被收录于专栏

本专栏主要是牛客题霸习题的讲解,有详细的考点分类,大家可以可以看看呦!!!

全部评论

相关推荐

暴杀流调参工作者:春招又试了一些岗位,现在投递很有意思,不仅要精心准备简历,投递官网还得把自己写的东西一条一条复制上去,阿里更是各个bu都有自己的官网,重复操作无数次,投完简历卡完学历了,又该写性格测评、能力测评,写完了又要写专业笔试,最近还有些公司搞了AI辅助编程笔试,有些还有AI面试,对着机器人话也听不明白录屏硬说,终于到了人工面试又要一二三四面,小组成员面主管面部门主管面hr面,次次都没出错机会,稍有不慎就是挂。 卡学历卡项目卡论文卡实习什么都卡,没有不卡的😂
点赞 评论 收藏
分享
刚刷到字节跳动官方发的消息,确实被这波阵仗吓了一跳。在大家还在纠结今年行情是不是又“寒冬”的时候,字节直接甩出了史上规模最大的转正实习计划——ByteIntern。咱们直接看几个最硬的数,别被花里胡哨的宣传词绕晕了。首先是“量大”。全球招7000多人是什么概念?这几乎是把很多中型互联网公司的总人数都给招进来了。最关键的是,这次的资源分配非常精准:研发岗给了4800多个Offer,占比直接超过六成。说白了,字节今年还是要死磕技术,尤其是产品和AI领域,这对于咱们写代码的同学来说,绝对是今年最厚的一块肥肉。其次是大家最关心的“转正率”。官方直接白纸黑字写了:整体转正率超过50%。这意味着只要你进去了,不划水、正常干,每两个人里就有一个能直接拿校招Offer。对于2027届(2026年9月到2027年8月毕业)的同学来说,这不仅是实习,这简直就是通往大厂的快捷通道。不过,我也得泼盆冷水。坑位多,不代表门槛低。字节的实习面试出了名的爱考算法和工程实操,尤其是今年重点倾斜AI方向,如果你简历里有和AI相关的项目,优势还是有的。而且,转正率50%也意味着剩下那50%的人是陪跑的,进去之后的考核压力肯定不小。一句话总结:&nbsp;27届的兄弟们,别犹豫了。今年字节这是铁了心要抢提前批的人才,现在投递就是占坑。与其等到明年秋招去千军万马挤独木桥,不如现在进去先占个工位,把转正名额攥在手里。
喵_coding:别逗了 50%转正率 仔细想想 就是转正与不转正
字节7000实习来了,你...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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