File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed 
00-code(源代码)/src/com/hi/dhl/algorithms/leetcode/_025/kotlin Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change 1+ package  com.hi.dhl.algorithms.leetcode._025.kotlin 
2+ 
3+ import  com.hi.dhl.algorithms.model.ListNode 
4+ 
5+ /* *
6+  * <pre> 
7+  *     author: dhl 
8+  *     date  : 2022/6/24 
9+  *     desc  : 
10+  * </pre> 
11+  */  
12+ class  Solution  {
13+ 
14+     fun  reverseKGroup (head :  ListNode ? , k :  Int ): ListNode ?  {
15+         if  (head ==  null ) {
16+             return  head
17+         }
18+         val  len =  getListLength(head)
19+         val  dumpHead =  ListNode (0 )
20+         dumpHead.next =  head
21+         var  p =  dumpHead
22+         var  q =  dumpHead.next
23+         var  index =  0 
24+         while  (q !=  null ) {
25+             if  (len -  index <  k) {
26+                 return  dumpHead.next
27+             }
28+             index =  index +  k
29+             var  m =  k;
30+             while  (-- m !=  0 ) {
31+                 val  node =  q.next
32+                 q.next =  q.next.next
33+                 node.next =  p.next
34+                 p.next =  node
35+             }
36+             p =  q
37+             q =  q.next
38+         }
39+         return  dumpHead.next
40+     }
41+ 
42+     private  fun  getListLength (head :  ListNode ? ): Int  {
43+         var  cur =  head
44+         var  len =  0 
45+         while  (cur !=  null ) {
46+             cur =  cur?.next
47+             len++ 
48+         }
49+         return  len
50+     }
51+ }
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments