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 }
  • 相关阅读:
    Ubuntu开机等待5分钟的取消方法
    329. 矩阵中的最长递增路径
    关于c语言中NULL的数值是否可以被修改
    #pragam在c++(visual studio 2019)编译器中的使用
    当cpu占有率过高时-sleep(0)的妙用
    inline解析
    一、【pytest实战--Web测试】搭建环境
    用openssl aes256 api实现文件加解密-带例程,兼容openssl enc -aes-256-cbc命令
    kali openvas安装
    C++关于变量初始化的琐记
  • 原文地址:https://www.cnblogs.com/birdhack/p/4174950.html
Copyright © 2011-2022 走看看