LeetCode-25. Reverse Nodes in k-Group

一、问题描述

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.

k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple ofk then left-out nodes in the end should remain as it is.

You may not alter the values in the nodes, only nodes itself may be changed.

Only constant memory is allowed.

For example,
Given this linked list: 1->2->3->4->5

For k = 2, you should return: 2->1->4->3->5

For k = 3, you should return: 3->2->1->4->5

二、解题思路

该问题是交换相邻两个链表元素的升级版

1、本人的思路是利用LinkedList构建队列,FILO,将链表的需要反转的k个元素添加到队列中,需要记录原链表中这k个元素的上一个元素cur和下一个元素head,然后不断从队列尾弹出元素,添加到cur的后面,并将cur指针右移,以此类推,直到队列中的元素全部弹出,然后将cur的下一个元素指向head。

2.使用单链表反转链表法(单链表反转链表主要参考http://blog.csdn.net/guyuealian/article/details/51119499)

(1)采用递归实现单链表反转

如图所示,执行head.next.next=head,将4的下一个元素指向3,返回上层函数后,head指向2,执行完head.next.next=head后,将3的下一个元素指向2,以此类推

(2)利用遍历实现单链表反转


三、Java代码

1、队列解法

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        if(k<=1)
            return head;
        ListNode result=new ListNode(0);
        result.next=head;
        ListNode cur=result;
        LinkedList<ListNode> queue=new LinkedList<ListNode>();
        int count=0;
        while(head!=null){
            queue.add(head);
            head=head.next;
            count++;
            if(count==k){
                while(queue.peekLast()!=null){
                    cur.next=queue.pollLast();
                    cur=cur.next;
                }
                cur.next=head;
                count=0;
            }
        }
        return result.next;
    }
}

2.反转链表(迭代)

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
     ListNode nextFirst;
    public ListNode reverseKGroup(ListNode head, int k) {
        if(k<=1)
            return head;
        int count=0;
        ListNode result=new ListNode(0);
        result.next=head;
        ListNode pre=result;
        while(head!=null){
            head=head.next;
            count++;
        }
        if(k>count)
            return result.next;
       head=result.next;
        for(int i=count;i>=k;i=i-k){
            pre.next=reverseNode(head,1,k);
            pre=head;
            head=nextFirst;
        }
        pre.next=head;
        return result.next;
    }
    private ListNode reverseNode(ListNode head,int num,int k){//num指向当前反转块的第num个元素
        if(num==k){
            nextFirst=head.next;
            return head;
        }
        ListNode reHead=reverseNode(head.next,++num,k);
        head.next.next=head;
        return reHead;
    }
    private ListNode reverse2(ListNode head,int k){
        int num=1;
        if(head==null)
            return head;
        ListNode cur=head.next;
        while(num<k){
            ListNode tem=cur.next;
            cur.next=head;
            head=cur;
            cur=tem;
            num++;
        }
        nextFirst=cur;
        return head;
    }
}

3.反转链表(遍历)

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
     ListNode nextFirst;//存储反转块的下一个元素
    public ListNode reverseKGroup(ListNode head, int k) {
        if(k<=1)
            return head;
        int count=0;
        ListNode result=new ListNode(0);
        result.next=head;
        ListNode pre=result;//存储反转块的上一个元素
        while(head!=null){
            head=head.next;
            count++;
        }
        if(k>count)
            return result.next;
       head=result.next;
        for(int i=count;i>=k;i=i-k){
            pre.next=reverse2(head,k);
            pre=head;
            head=nextFirst;
        }
        pre.next=head;
        return result.next;
    }
    private ListNode reverseNode(ListNode head,int num,int k){
        if(num==k){
            nextFirst=head.next;
            return head;
        }
        ListNode reHead=reverseNode(head.next,++num,k);
        head.next.next=head;
        return reHead;
    }
    private ListNode reverse2(ListNode head,int k){
        int num=1;
        if(head==null)
            return head;
        ListNode cur=head.next;
        while(num<k){
            ListNode tem=cur.next;
            cur.next=head;
            head=cur;
            cur=tem;
            num++;
        }
        nextFirst=cur;
        return head;
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值