Advertisement
unknown_0711

Untitled

Apr 23rd, 2023
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. public class Solution {
  2. public ListNode reverseKGroup(ListNode head, int k) {
  3. if (head==null||head.next==null||k<2) return head;
  4.  
  5. ListNode dummy = new ListNode(0);
  6. dummy.next = head;
  7.  
  8. ListNode tail = dummy, prev = dummy,temp;
  9. int count;
  10. while(true){
  11. count =k;
  12. while(count>0&&tail!=null){
  13. count--;
  14. tail=tail.next;
  15. }
  16. if (tail==null) break;
  17.  
  18.  
  19. head=prev.next;
  20. while(prev.next!=tail){
  21. temp=prev.next;
  22. prev.next=temp.next;
  23.  
  24. temp.next=tail.next;
  25. tail.next=temp;
  26.  
  27. }
  28.  
  29. tail=head;
  30. prev=head;
  31.  
  32. }
  33. return dummy.next;
  34.  
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement