diff --git a/algorithms/CPlusPlus/Linked-Lists/Reverse_nodes_in_k_groups.cpp b/algorithms/CPlusPlus/Linked-Lists/Reverse_nodes_in_k_groups.cpp new file mode 100644 index 00000000..45601fde --- /dev/null +++ b/algorithms/CPlusPlus/Linked-Lists/Reverse_nodes_in_k_groups.cpp @@ -0,0 +1,33 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + int length(ListNode *head){ + int len=0; + while(head){ + len++; + head=head->next; + } + return len; + } + ListNode* reverseKGroup(ListNode* head, int k) { + if(length(head)next; + curr->next=prev; + prev=curr; + curr=n; + } + head->next=reverseKGroup(curr,k); + return prev; + } +};