From 32004e2a6dd24c10fc7653903b1a74c663d2bd3b Mon Sep 17 00:00:00 2001 From: Harshit Muhal <42070734+harshitmuhal@users.noreply.github.com> Date: Fri, 22 May 2020 19:29:11 +0530 Subject: [PATCH 1/2] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b6b10a3..f907597 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # InterviewBit -InterviewBit Programming Solutions. +InterviewBit Programming Solutions! https://www.interviewbit.com From c1be2b214de85b64c3cea43024bfacdd1f7f7e11 Mon Sep 17 00:00:00 2001 From: Harshit Muhal <42070734+harshitmuhal@users.noreply.github.com> Date: Thu, 1 Oct 2020 12:56:50 +0530 Subject: [PATCH 2/2] added new question on LinkedList from InterviewBit --- LinkedLists/ReverseAlternateKNodes.cpp | 58 ++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 LinkedLists/ReverseAlternateKNodes.cpp diff --git a/LinkedLists/ReverseAlternateKNodes.cpp b/LinkedLists/ReverseAlternateKNodes.cpp new file mode 100644 index 0000000..93f8c92 --- /dev/null +++ b/LinkedLists/ReverseAlternateKNodes.cpp @@ -0,0 +1,58 @@ +/*Given a linked list A of length N and an integer B. + +You need to reverse every alternate B nodes in the linked list A. + +LINK - https://www.interviewbit.com/problems/reverse-alternate-k-nodes/ + +Example Input +Input 1: + + A = 3 -> 4 -> 7 -> 5 -> 6 -> 6 -> 15 -> 61 -> 16 + B = 3 +Input 2: + + A = 1 -> 4 -> 6 -> 6 -> 4 -> 10 + B = 2 + +Example Output +Output 1: + + 7 -> 4 -> 3 -> 5 -> 6 -> 6 -> 16 -> 61 -> 15 +Output 2: + + 4 -> 1 -> 6 -> 6 -> 10 -> 4 + */ + +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ + +ListNode* Solution::solve(ListNode* A, int B) { + if (A == NULL || A->next == NULL) { + return A; + } + ListNode* tail = A, *head; + ListNode* curr = A, *prev, *nxt; + for (int i = 0; i < B; i++) { + nxt = curr->next; + curr->next = prev; + prev = curr; + curr = nxt; + } + head = prev; + if (curr) { + tail->next = curr; + for (int i = 0; i < B; i++) { + tail = curr; + curr = curr->next; + } + } + ListNode* temp_head = solve(curr, B); + tail->next = temp_head; + return head; +} \ No newline at end of file