单链表每k个节点一组进行反转(最后不足k个也反转)

本文围绕一道面试题展开,需在10分钟内手写代码实现单链表每k个一组反转,作者起初未完成,后续梳理过程发现并不难。思路是在单链表反转基础上加控制参数、记录关键节点,还给出了自定义Node类及实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一道面试题,第一次碰到这道题的时候 要求10分钟之内手写代码实现,当时没写出来,后来花点时间把过程梳理一遍,也挺简单的.......

思路就是在原来单链表反转的基础上,加几个控制参数,记录几个关键节点。

每k个为一组。

 

先自定义一个Node类:

public class Node {
    private int data;
    private Node next;

    public Node(int data, Node next) {
        this.data = data;
        this.next = next;
    }

    public Node() {
    }

    @Override
    public String toString() {
        return "Node{" +
                "data=" + data +
                ", next=" + next +
                '}';
    }

    public int getData() {
        return data;
    }

    public void setData(int data) {
        this.data = data;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }
}

 

实现代码:

public class LinkReverse2 {

    public static Node newLink(int n){
        Node head = new Node();
        head.setData(1);
        head.setNext(null);
        Node tmp = head;
        for(int i=2;i<=n;i++){
            Node newNode = new Node();
            newNode.setData(i);
            newNode.setNext(null);
            tmp.setNext(newNode);
            tmp = newNode;
        }
        return head;
    }

    public static void main(String[] args) {
        Node node = newLink(10);
        pritNode(node);
        Node node1 = reverseKLink(node,3);
        pritNode(node1);

    }
    public static void pritNode(Node head){
        Node temp = head;
        while(temp !=null){
            System.out.print(temp.getData()+"->");
            temp = temp.getNext();
        }
        System.out.println();
    }

    /*public static Node reverseLink(Node head){
        Node pre=null,cur=head,next=null;
        while(cur!=null){
            next=cur.getNext();
            cur.setNext(pre);
            pre=cur;
            cur=next;
        }
        return pre;
    }*/


    public static Node reverseKLink(Node head,int k){
        Node pre=null,cur=head,next=null;

        Node pnext=null,global_head=null;
        int i=0;
        Node tmp=null;

        while(cur!=null){
            next = cur.getNext();

            if(tmp==null) tmp=cur;   //tmp记录当前组反转完最后一个节点
            if(pnext!=null) pnext.setNext(cur);  //pnext是上一组反转完最后一个节点

            cur.setNext(pre);
            pre=cur;
            cur = next;

            i++;
            if(i>=k){  //当前组反转完成的时候
                if(global_head==null){
                    global_head=pre;
                }
                if(pnext!=null){  //将上一组反转完的最后一个节点指向 当前组反转完后的第一个节点
                    pnext.setNext(pre);
                }
                pnext=tmp; //迭代

                i=0;  //新的一组反转时 关键数据初始化
                tmp=null;
                pre=null;
            }
        }
        return global_head;
    }
}

 

思路图:

转载于:https://www.cnblogs.com/f-society/p/10812428.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值