Question: Write a program to reverse a Linked List in pairs. That means we should reverse the 1st and 2nd numbers, 3rd and 4th numbers and so on. Input: 4, 8, 15, 16, 23, 42 Output: 8, 4, 16, 15, 42, 23 The basic idea behind solving this problem is that we need to traverse the entire list and we need to keep on swapping the values of 2 adjacent nodes. Since we need to reverse the values in pairs, it is also clear that we need to move 2 …
Tag:
Recursion
Algorithms involving recursion
-
-
Question: Print the Linked List in reverse order. The actual structure of the Linked List must remain intact. Input: 4, 8, 15, 16, 23, 42 Output: 42, 23, 16, 15, 8, 4 We just discussed how to reverse a Linked List in an older post. Reverse a singly Linked List. But, in the above described method we changed the entire structure of the Linked List, the last node was no longer the last node and the head node had also changed. We might encounter cases, where we just need to …