"Leetcode-Interview algorithm classic-java Implementation" "025-reverse Nodes in K-group (a set of K nodes in a single linked list to invert)"

Source: Internet
Author: User

"025-reverse Nodes in K-group (a set of K nodes in a single linked list is reversed)" "leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index" Original Question

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, and only nodes itself is 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

Main Topic

Given a single-linked list, and a group of K, each K-node is reversed, if the last node is less than k to keep the original link order unchanged.

Thinking of solving problems

Record the last node of the linked list (tail) with a pointer, record the last node (head) of the linked part with a pointer to the node at the head of the non-linked list, insert the K element, and then move head to tail, tail re-record the tail node of the linked list Until all the elements have been manipulated. If the last set of elements is less than k, because the tail interpolation method, so also to restore, the final head element of the tail interpolation method can be

Code Implementation

Node class

publicclass ListNode {    int val;    ListNode next;    ListNode(int x) {        val = x;    }}

Algorithm implementation class

 Public  class solution {     PublicListNode Reversekgroup (ListNode head,intK) {if(k <=1) {returnHead } ListNode root =NewListNode (0);//The precursor of the first element of a groupListNode grouphead = root;//Current node to be processedListNode Curr = head;///processed tail node of the linked listListNode grouptail = head;//The successor of the node currently to be processedListNode Next;//How many nodes are processed for each group        int Count=0; while(Curr! =NULL) {//If it is the first element of a group, record it            if(Count==0) {grouptail = Curr; }//record number of elements processed            Count++;//Record the next node to be processed.Next = Curr.next;//operation by using the tail interpolation methodCurr.next = Grouphead.next;            Grouphead.next = Curr; Curr = Next;//has finished processing K nodes, the precursor of the packet head moves to the last linked node .            if(Count= = k) {grouphead = Grouptail;//Counter zeroed                Count=0; }        }//indicates that the number of nodes is not an integral multiple of k, and will not be the node of the element at the end of the integer multiple.        //Use the tail interpolation method again to restore        if(Count!=0) {Curr = Grouphead.next; Grouphead.next =NULL; while(Curr! =NULL) {next = Curr.next;                Curr.next = Grouphead.next;                Grouphead.next = Curr;            Curr = Next; }        }returnRoot.next; }}
Evaluation Results

  Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.

Special Instructions Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47034983"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Leetcode-Interview algorithm classic-java Implementation" "025-reverse Nodes in K-group (a set of K nodes in a single linked list to invert)"

Related Article

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.