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 }
  • 相关阅读:
    IBM Lotus网站荟萃
    Lotus 深入浅出系列——前言(二)IBM Lotus开发人员的几个境界
    在IIS上配置和测试Perl脚本
    网站推广
    iTOP开发板MiniLinuxC程序调用shell命令
    iTOP4412开发板_驱动_adc驱动升级和测试例程
    最近想入手树莓派3来学习编程同学没有选择4412开发板吗?
    iTOP4412开发板串口转接小板的使用文档
    学习嵌入式4412开发板手把手配套视频_2000人群组在线交流
    电子医疗设备创新研发应该用i.MX6Q开发板吗?为医疗设备提供解决方案
  • 原文地址:https://www.cnblogs.com/birdhack/p/4174950.html
Copyright © 2011-2022 走看看