zoukankan      html  css  js  c++  java
  • LeetCode 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.

    If the number of nodes is not a multiple of k 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 /**
      2  * Definition for singly-linked list.
      3  * public class ListNode {
      4  *     int val;
      5  *     ListNode next;
      6  *     ListNode(int x) {
      7  *         val = x;
      8  *         next = null;
      9  *     }
     10  * }
     11  */
     12 public class Solution {
     13     public ListNode reverseKGroup(ListNode head, int k) {
     14 
     15         if (head==null) {
     16             return null;
     17         }
     18         if (k == 1) {
     19             return head;
     20         }
     21 
     22         ListNode temp=head;
     23         ListNode doRe = head, nextList = null, first = null, last = null;
     24         int i=1;
     25 
     26         while (temp != null) {
     27 
     28             if (i % k == 0) {
     29                 if (i == k) {
     30                     first = reverseBetween(doRe, i / k, i);
     31 
     32                 } else {
     33                     last.next = reverseBetween(doRe, 1, k);
     34                 }
     35                 last = doRe;
     36                 temp = doRe.next;
     37                 doRe = doRe.next;
     38             } else {
     39             //    last = temp;
     40                 temp = temp.next;
     41             }
     42 
     43             ++i;
     44         }
     45         return first==null?doRe:first;
     46 
     47     }
     48     
     49     
     50     public ListNode reverseBetween(ListNode head, int m, int n) {
     51         if (head==null) {
     52             return null;
     53         }
     54         if(n==m){
     55             return head;
     56         }
     57         ListNode temp=head,doRe = null,second=null,first=null,last=null;
     58         int i=1;
     59 
     60         while (temp!=null) {
     61             if (i==m-1) {
     62                 first=temp;
     63             }
     64             if (i==m) {
     65                 doRe=temp;
     66             }
     67             if (i==n) {
     68                 last=temp;
     69                 second=temp.next;
     70                 if(first!=null) first.next=last;
     71                 last.next=null;
     72 
     73                 break;
     74             }
     75             ++i;
     76             temp=temp.next;
     77         }
     78         reverse(doRe);
     79         doRe.next=second;    
     80 
     81         if (m>1) {
     82             return head;
     83         }else {
     84             return last;
     85         }
     86 
     87     }
     88     private void reverse(ListNode head) {
     89         ListNode pre = null,curr,next;
     90 
     91         curr=head;
     92         if (head==null) {
     93             return;
     94         }
     95         while (curr!=null) {
     96             next=curr.next;
     97             curr.next=pre;
     98             pre=curr;
     99             curr=next;
    100         }
    101     }
    102 
    103 
    104 
    105 }
  • 相关阅读:
    容器平台选型的十大模式:Docker、DC/OS、K8S 谁与当先?
    Spring Controller里注入Feign的Interface报红提示的问题
    几种常见的日志
    个人博客搭建方案选择
    elasticsearch常用操作命令
    kafka操作命令
    centos7.0安装java环境
    CentOS安装jdk的三种方法
    在OAuth2中 自定义tokenServices来提供个性化服务,每次刷新token并让原token在5分钟内有效
    解决HttpServletRequest的输入流只能读取一次的问题(转)
  • 原文地址:https://www.cnblogs.com/birdhack/p/4174950.html
Copyright © 2011-2022 走看看