[Leetcode click Notes] Reverse nodes in K-group

Source: Internet
Author: User

Given a linked list, reverse the nodes of a linked listKAt a time and return its modified list.

If the number of nodes is not a multipleKThen left-out nodes in the end shoshould 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

ForK= 2, you shoshould return:2->1->4->3->5

ForK= 3, you shoshould return:3->2->1->4->5

 

Question: The idea is simple. You can do it step by step. In addition to the functions required by the question, two functions are implemented:

Private listnode [] reversesub (listnode head, int K) This function reverts the chain table whose head points to K and returns an array of 2, the first element in the array is the head node of the reverse linked list, and the second element is the end node of the reverse linked list.

Private int getlength (listnode head) This function returns the length of the chain table pointed to by the head.

In the reversekgroup function, first calculate the length Len of the original linked list, then the number of groups to be reversed is Len/K, and then call the reversesub function Len/K to reverse each chain table, then string them based on the returned first and last pointers. Finally, judge whether there are elements in the linked list that do not need to be reversed based on whether Len % K is 0. If so, link them to the end of the linked list and return them.

The Code is as follows:

 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     private ListNode[] reverseSub(ListNode head,int k){14         ListNode[] answer = new ListNode[2];15         answer[1] = head;16         ListNode prev = null;17         for(int i = 0;i < k;i++){18             ListNode temp = head.next;19             head.next = prev;20             prev = head;21             head = temp;22         }23         answer[0] = prev;24         return answer;25     }26     private int getlength(ListNode head){27         int count = 0;28         while(head != null){29             count ++;30             head = head.next;31         }32         return count;33     }34 35     public ListNode reverseKGroup(ListNode head, int k) {36         int len = getlength(head);37         if(len < k)38             return head;39         40         ListNode answer = null;41         ListNode tail = new ListNode(0);42         int for_num = len / k;43         for(int i = 0;i < for_num;i++){44             ListNode h = head;45             46             //find next starter47             for(int j = 0;j < k;j++)48                 head = head.next;49             50             ListNode[] temp = reverseSub(h, k);51             if(answer == null){52                 answer = temp[0];53                 tail = temp[1];54             }55             else {56                 tail.next = temp[0];57                 tail = temp[1];58             }                        59         }60         61         if(len%k != 0)62             tail.next = head;63         else64             tail.next = null;65         66         return answer;67     }68 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.