Leetcode (+) Reverse Nodes in K-group (linked list)

Source: Internet
Author: User

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

The work of the list is examined. For the title, enter a list and a number k, and the number K represents a reversal of every k digit in the list. The topic requires that the requirement be fulfilled by changing the node location, but not by changing the value of the node to achieve the goal (changing value is obviously a cheat O (╯-╰) o).

Solution Ideas

This topic is still relatively simple, as follows:

Save the node to be reversed to the stack, and save the previous pointer priv and the next pointer next, starting with the Priv pointer, and then linking the node from the stack to the next node of the last stack node.

Places to be aware of:

    • When the first reversal is Priv = NULL, the pointer at the top of the stack needs to be set to the head pointer of the linked list.
    • In addition, for the last time the list is dissatisfied with k elements, the number of nodes in the stack is less than k, is not reversed operation, so this method does not affect the end of the linked list node.

The code is as follows:

/** * Definition forsingly-linked list. * struct ListNode {*intVal * ListNode *Next; * ListNode (intx): Val (x),Next(NULL) {} * }; */classSolution { Public: listnode* Reversekgroup (listnode* head,intK) {if(!head | | k <=1) return head;        listnode* h = head; listnode* Priv =NULL;        Stack<listnode*> Stacknodes;        listnode* L1 = head; listnode* L2 = head;intCount =0; while(L2) {if(Count! = k1) {count++;                Stacknodes.push (L2); L2 = l2->Next; }Else{L1 = L2; L2 = l2->Next; Count =0;if(PRIV) priv->Next= L1;Elseh = L1; while(!stacknodes.Empty()) {l1->Next= Stacknodes.top (); L1 = l1->Next;                Stacknodes.pop ();                } priv = L1; Priv->Next= L2;    }} return H; }};

Leetcode (+) Reverse Nodes in K-group (linked list)

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.