Linked List Problem - K reverse linked list
-
Hi, I need help in solving the Linked List K reverse linked list problem.
Given a singly linked list and an integer K, reverses the nodes of the
list K at a time and returns modified linked list.
NOTE : The length of the list is divisible by K
Example :Given linked list 1 -> 2 -> 3 -> 4 -> 5 -> 6 and K=2,
You should return 2 -> 1 -> 4 -> 3 -> 6 -> 5
Try to solve the problem using constant extra space.
Can anyone point me in the right direction? Thanks.
-
Hi, I need help in solving the Linked List K reverse linked list problem.
Given a singly linked list and an integer K, reverses the nodes of the
list K at a time and returns modified linked list.
NOTE : The length of the list is divisible by K
Example :Given linked list 1 -> 2 -> 3 -> 4 -> 5 -> 6 and K=2,
You should return 2 -> 1 -> 4 -> 3 -> 6 -> 5
Try to solve the problem using constant extra space.
Can anyone point me in the right direction? Thanks.
-
Hi, I need help in solving the Linked List K reverse linked list problem.
Given a singly linked list and an integer K, reverses the nodes of the
list K at a time and returns modified linked list.
NOTE : The length of the list is divisible by K
Example :Given linked list 1 -> 2 -> 3 -> 4 -> 5 -> 6 and K=2,
You should return 2 -> 1 -> 4 -> 3 -> 6 -> 5
Try to solve the problem using constant extra space.
Can anyone point me in the right direction? Thanks.
Try to break the problem into sub-problems. For example: 1. Given a singly-linked list of length k, reverse the order of the elements 2. Given a singly-linked list of length 2*k, reverse the order of the first k elements, without touching the last k elements. Return a pointer to the _k_th element (counting from zero). 3. Given a singly-linked list of length 2*k, reverse the order of the last k elements. 4. Given a singly-linked list of length 3*k, reverse the order of the middle k elements. return a pointer to the _2*k_th element (counting from zero). 5. Put everything together to get the solution to your problem.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.
-
The right direction is forward. :rolleyes: Using an array of
K
pointers would make the trick.CPallini wrote:
Using an array of
K
pointersWouldn't that break the condition to only use a constant amount of extra space (assuming this refers to memory usage)?
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
-
Hi, I need help in solving the Linked List K reverse linked list problem.
Given a singly linked list and an integer K, reverses the nodes of the
list K at a time and returns modified linked list.
NOTE : The length of the list is divisible by K
Example :Given linked list 1 -> 2 -> 3 -> 4 -> 5 -> 6 and K=2,
You should return 2 -> 1 -> 4 -> 3 -> 6 -> 5
Try to solve the problem using constant extra space.
Can anyone point me in the right direction? Thanks.
Break the problem into simpler tasks and implement functions for these: 1. write a function that reverses a full list. Make sure to adhere to the constraint to only use a constant amount of space (memory). 2. write a function to find the Nth element within a list from a given starting position (not actually needed, but will simplify the final program) 3. write a function to split off part of a given list before the Nth position 4. write code to retrieve sub-lists of length K using 2. and 3. and code to insert a (modified) sublist back at the original position. 5. use 1. and 4. in an iteration to process the entire list All of these steps can easily be done with the help of at most two or three temporary references (pointers). The only tricky one is the reverse function.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
-
Hi, I need help in solving the Linked List K reverse linked list problem.
Given a singly linked list and an integer K, reverses the nodes of the
list K at a time and returns modified linked list.
NOTE : The length of the list is divisible by K
Example :Given linked list 1 -> 2 -> 3 -> 4 -> 5 -> 6 and K=2,
You should return 2 -> 1 -> 4 -> 3 -> 6 -> 5
Try to solve the problem using constant extra space.
Can anyone point me in the right direction? Thanks.
Member 14055543 wrote:
Can anyone point me in the right direction?
What code do you have so far? What help have you sought from your instructor? What does the problem/solution look like on paper (don't bother with code until you can complete this step)?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
CPallini wrote:
Using an array of
K
pointersWouldn't that break the condition to only use a constant amount of extra space (assuming this refers to memory usage)?
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)