Leetcode--Reverse Nodes in K-group

Source: Internet
Author: User

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

Analysis:

This is an upgraded version of the swap nodes, which divides the nodes in the list into K-groups, with the order of the nodes in each group inverted, but the order of the groups is the same, and if the number of remaining nodes is less than k, it is not reversed.

The idea is to first generate a head node to prevent the reverse chain from being found. Then set three nodes, a start, point to the chain of the next list of links, a p, point to the chain of the list of links, a Q, look for the end of the list of the group; a Val indicating that the group has traversed several nodes at this time. At the same time, a global pointer is needed throughout the program to facilitate the search for the end of the chain list. In this way, the list of linked lists that p points to simply calls the function in place upside down, then joins the chain end of the return chain.

Answer:

/*** Definition for singly-linked list. * public class ListNode {* int val; * ListNode Next; * ListNode (int X) {val = x;}}*/ Public classSolution {PrivateListNode Tail;  PublicListNode Reversekgroup (ListNode head,intk) {if(Head = =NULL|| Head.next = =NULL)             returnHead; if(k <= 1)            returnHead; //Create a first nodeListNode h =NewListNode (-1); Tail=h; ListNode Start= head, p = head, q = head;//start points to the current link to reverse the node, Q traversal node to find K nodes         while(Q! =NULL|| Q.next! =NULL) {            intval = 1;  while(Val < k && Q.next! =NULL) {Q=Q.next; Val++; }            if(Val < k && Q.next = =NULL) {Tail=GetTail (); Tail.next=start; returnH.next; } Start=Q.next; Q.next=NULL; ListNode jion=reverselist (P); Tail=GetTail (); Tail.next=jion; if(Start = =NULL) {                returnH.next; } P=start; Q=start; }                returnH.next; }                 PublicListNode GetTail () { while(Tail.next! =NULL) Tail=Tail.next; returntail; }         PublicListNode reverselist (ListNode head) {if(Head = =NULL|| Head.next = =NULL)            returnHead; ListNode h=Head;  while(Head.next! =NULL) {ListNode P=Head.next; Head.next=P.next; P.next=h; H=p; }        returnh; }}

You are here!
Your runtime beats 98.99% of Java submissions.

(Haha, feel the list of problems almost OK, you can not look at other people's thinking, the final runtime will be more than 98%, a good sense of accomplishment ~ but the tree, graph and other data structures are still very poor, to practice more! )

Leetcode--Reverse Nodes in K-group

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.