LeetCode -- Reverse Nodes in k-Group

Source: Internet
Author: User

LeetCode -- Reverse Nodes in k-Group
Description:


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 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


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


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


If the number of nodes from the current node is greater than k in the Process of the traversal chain table, the k nodes are reversed, and then k locations are moved backward from the current position, continue to reverse until the number of nodes from the current node to the end is less than K.


Ideas:
1. Create a reversal function to reverse the k nodes from the current start. Space complexity of this function O (n)
2. Get the total length of the linked list first. If it is smaller than k, return directly. Otherwise, k nodes are rotated each time and pointed to the position of the next rotation.




Implementation Code:

/** * Definition for singly-linked list. * public class ListNode { *     public int val; *     public ListNode next; *     public ListNode(int x) { val = x; } * } */public class Solution {    public ListNode ReverseKGroup(ListNode head, int k)     {        if(head == null || head.next == null || k == 1){    return head;    }        var len = 0;    var h = head;    while(head != null){    head = head.next;    len ++;    }        if(k > len){    return h;    }        ListNode pre = null;    ListNode newHead = null;    while(len >= k)    {    ReverseKNodes(ref h, k, ref pre);        if(pre == null){    newHead = h;    pre = h;    for(var i =0 ;i < k-1; i++){    pre = pre.next;    }    }else{    for(var i = 0;i < k;i++){    pre = pre.next;    }    }        for(var i = 0;i < k;i++){    h = h.next;    }        len -= k;    }        return newHead;    }private void ReverseKNodes(ref ListNode n, int k, ref ListNode preNode){var stack = new Stack
 
  ();for(var i = 0;i < k; i++){stack.Push(n.val);n = n.next;}ListNode end = n;ListNode tmp = new ListNode(stack.Pop());var tmpHead = tmp;while(stack.Count > 0){var n1 = new ListNode(stack.Pop());tmp.next = n1;tmp = tmp.next;}tmp.next = end;if(preNode != null){n = tmpHead;preNode.next = tmpHead;}else{n = tmpHead;}}}
 


 

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.