I’m facing errors here.
kindly help me to resolve the error
Reverse the linked list
hi @dips123deepali_c25f140838182212
u have sent the code for merging two sorted linked lists… its working fine
what issue are u facing??
why are we using type void for creating reverse linked list and not node type
Kindly share ur code… u have sent code for merging linked lists…
hi @dips123deepali_c25f140838182212
why are u again and again sending the code for merging the linked lists…
The need of the question is to reverse every k nodes (where k is an input to the function) and k is a factor of size of List.
Example:
Inputs: 1->2->3->4->5->6->7->8->NULL and k = 3
Output: 3->2->1->6->5->4->8->7->NULL.
Algorithm :
- Reverse the first sub-list of size k.1.1 While reversing keep track of the next node and previous node.1.2 Let the pointer to the next node be next and pointer to the previous node be prev.
- head->next = reverse(next, k) /* Recursively call for rest of the list and link the two sub-lists */
- return prev /* prev becomes the new head of the list (see the diagrams of iterative method of this post) */
Refer this–>
Node reverse(Node head, int k)
{
Node current = head;
Node next = null;
Node prev = null;
int count = 0;
/* Reverse first k nodes of linked list */
while (count < k && current != null)
{
next = current.next;
current.next = prev;
prev = current;
current = next;
count++;
}
/* next is now a pointer to (k+1)th node
Recursively call for the list starting from current.
And make rest of the list as next of first node */
if (next != null)
head.next = reverse(next, k);
// prev is now head of input list
return prev;
}
I included reverse function in the merge sort function code but now I’ve seperated them still I’m facing error.
hi @dips123deepali_c25f140838182212
in input u have to first input n then k then n elements…
but u are inputting k after inputting n elements… so just correct it…
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.