Leetcode Reverse Nodes in K-group K a set of reversal nodes

Source: Internet
Author: User

title :

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

translation :

Give you a list of the inverted nodes that have K as a group. Like K for 2 times, 2 a set of reversals.

Ideas :

It's not the same idea as the previous question. Because K is not sure, it cannot be like swapping 2 nodes, directly marking what the next node is in Exchange.

The way to think about this is to reverse the list. That is, given a linked list, turn all of his nodes upside down.

Then only need to the original topic to the list by K group. Then turn each group upside down. and returns the last node after the rollover.

Code :

  public static ListNode Reversekgroup (ListNode head, int k) {if (head = = NULL) return null;     ListNode root = new ListNode (0);     Root.next = head;     ListNode pre = root;     int count = 0;     ListNode temp = null; while (head! = null) {count++;//count if (count = = k)//to K processing {temp= head.next;//@ 1 pre = reverse (pre , head.next);//pre for the list of the previous one, Head.next for the latter one.     That is, the list of this interval is reversed by count = 0;    Head = temp;//@ 2} else head = head.next;//@ 3} return root.next; } public static ListNode reverse (ListNode Pre,listnode end) {ListNode last = Pre.next; ListNode cur = last.next;  while (cur!=end) {last.next = Cur.next; cur.next = pre.next; pre.next = cur; cur = last.next;} return last; }/** * @param args */public static void main (string[] args) {//TODO auto-generated method stub listnode a = new ListNode (1); ListNode B = new ListNode (2); ListNode C = new ListNode (3); ListNode d = new ListNode (4), a.next = B;b.next = C;c.next = D;d.next = null; ListNode P =reversekgroup (a,2); while (P!=null) {System.out.print (p.val+ "\ t");p = P.next;}} 
In the @1 and @2 place and the purpose of the @3 is the same. That is, the pointer points to the next node.

The reason is different because. @1 and @2 are reversed. Join the list of 1234. K is 2, at which point the head points to 2, but after the reversal, Head.next points to 1 instead of 3. So you should save the location information of the Head.next before reversing processing. At the end of processing, point the head to the save result.


The core of the problem is to reverse the list.

Paste a reverse linked list of core code, for reference only.

public class Reverselist {public static ListNode reverse (ListNode a) {ListNode root = new ListNode (0); root.next = A; ListNode cur = a.next;while (cur!=null) {a.next = Cur.next;   Suppose that 1234 at this point a points to 1,cur point 2, first make 1 the next 3, then Cur.next = Root.next;//2 next for 1,root is 2, cur points to 3. At this time for 2134root.next = cur;//when cur is 3 o'clock, first let 1 point to 4,3 point 2 root point 3 get 3214; cur =a.next;} return root.next;} /** * @param args */public static void main (string[] args) {//TODO auto-generated method stub listnode a = new ListNode (1); ListNode B = new ListNode (2); ListNode C = new ListNode (3); ListNode d = new ListNode (4), a.next = B;b.next = C;c.next = D;d.next = null; ListNode P =reverse (a); while (P!=null) {System.out.print (p.val+ "\ t");p = P.next;}}




Leetcode Reverse Nodes in K-group K a set of reversal nodes

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.