(Leetcode 25) Reverse Nodes in K-group

Source: Internet
Author: User

Title: Reverse Nodes in K-group

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


Thinking of solving problems:

First of all, the title means that given a linked list and a number k, the chain list with K nodes for a group to reverse, if not enough k nodes are not reversed. Mainly divided into the following steps to solve the problem:

1. Split the list with K nodes, and if enough k nodes are reversed

2. If the K nodes are not enough, do not reverse

3. Splicing phase, the reversal of a small Ching inserted in the middle of the list has been operated, if not enough k nodes are not operational.


See the following Java code (with detailed comments):

public class Solution {public ListNode Reversekgroup (ListNode head, int k) {if (head = = NULL | | head.next = = NULL) {RE Turn head;} ListNode p = head;        ListNode pre = NULL;        The pre records the trailing node of the previous half-list, Boolean flag = true;     Mark if is the Head node while (P! = null) {ListNode tail = p; The current is the head, but the reverse is not the head, into the tail int i;for (i = 1; i < K && P! = null; ++i) {p = P.next;}   ListNode nextlist = null;if (i >= k && p! = null) {nextlist = P.next;p.next = null; Place the last node of the current list next to Nulllistnode temphead = reverse (tail); if (flag = = True) {//If the header is processed head = Temphead;flag = False;tail . Next = nextlist;} else{//If not the head tail.next = Nextlist;pre.next = Temphead;}}     Pre = tail; Record the last node of a linked list P = nextlist;}    return head; }public ListNode Reverse (ListNode head) {if (head = = NULL | | head.next = NULL) {return head;} ListNode p = head, q = Head.next;head.next = Null;while (q! = null) {ListNode r = Q.next;q.next = P;p = Q;q = r;} Head = P;return head;}}


(Leetcode 25) 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.