reverse a linked list- DATA STRUCTURESWrite a program to reverse a linked list.Constraints:Integers onlyExample:Input FormatTo get the number of elements followed by the elements separated by a single space.Output FormatDisplays the reversed list.Note: There is a space at the end of the outputExplanation:linked listPublic Test Cases:# INPUT EXPECTED OUTPUT1 3 67 45 1212 45 672 5 -1 3 2 6 44 6 2 3 -1
Question
reverse a linked list- DATA STRUCTURESWrite a program to reverse a linked list.Constraints:Integers onlyExample:Input FormatTo get the number of elements followed by the elements separated by a single space.Output FormatDisplays the reversed list.Note: There is a space at the end of the outputExplanation:linked listPublic Test Cases:# INPUT EXPECTED OUTPUT1 3 67 45 1212 45 672 5 -1 3 2 6 44 6 2 3 -1
Solution
Here is a Python solution for reversing a linked list:
class Node:
def __init__(self, data=None):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def insert(self, data):
if not self.head:
self.head = Node(data)
else:
new_node = Node(data)
cur = self.head
while cur.next:
cur = cur.next
cur.next = new_node
def display(self):
elements = []
cur_node = self.head
while cur_node:
elements.append(cur_node.data)
cur_node = cur_node.next
return elements
def reverse(self):
prev = None
current = self.head
while current:
nxt = current.next
current.next = prev
prev = current
current = nxt
self.head = prev
# Test the code
llist = LinkedList()
llist.insert(67)
llist.insert(45)
llist.insert(12)
print("Original List: ", llist.display())
llist.reverse()
print("Reversed List: ", llist.display())
In this code, we first create a linked list with the LinkedList class. The insert method is used to add elements to the list. The display method is used to print the elements of the list.
The reverse method is where the reversal of the linked list happens. We initialize three pointers: prev, current, and nxt. We traverse the list, at each iteration, we reverse the next pointer of the current node to point to the prev node. Then, we move the prev and current pointers one step forward. At the end of the traversal, we point the head of the list to the prev pointer (which is now pointing at the last node), thus achieving the reversal of the linked list.
Similar Questions
Reversal ChallengeImagine you are working as an employee in an MNC, where your manager knows that you are very good at coding in arrays. He has provided you a vigorous training on data structures, so he challenges you to do the reversal of an elements using linked lists.Constraints:Input Format :The input contains a set of integers.Output Format :The output contains reversed set of given integers.Example:Input 1:51 2 3 4 5Output 1:5 4 3 2 1Input 2:411 12 14 13Output 2:13 14 12 11Explanation:Reverse the elements sequence in the given linked listPublic Test Cases:# INPUT EXPECTED OUTPUT1 5 1 2 3 4 5 5 4 3 2 12 4 11 12 14 1313 14 12 11
Given a linked list of size N. The task is to reverse every k nodes (where k is an input to the function) in the linked list.Input FormatFirst line of input contains n- the size of first linked list. Next line of input contains n integers the n elements of the first linked list.Constraints1 <= N <= 10^41 <= k <= NOutput FormatPrint the modified linked list.Sample input81 2 2 4 5 6 7 84Sample output4 2 2 1 8 7 6 5
Reverse a Doubly Linked ListReverse a Doubly Linked ListConstraints:NAExample:Sample Input:3123Sample Output:List before reversal: 1 2 3 List after reversal: 3 2 1 Explanation:Input: 3 (Size), 1 2 3 (Initial elements)Output: List before reversal: 1 2 3Output: List after reversal: 3 2 1Public Test Cases:# INPUT EXPECTED OUTPUT1 3123List before reversal: 1 2 3 List after reversal: 3 2 1
Write a C program to reverse a single linked list recursively. Sample Test Cases Test case 1 No·of·nodes:·5 Data·for·node·1:·5 Data·for·node·2:·4 Data·for·node·3:·3 Data·for·node·4:·2 Data·for·node·5:·1 Original·linked·list:·5·->·4·->·3·->·2·->·1·->·Null⏎ Reversed·linked·list:·1·->·2·->·3·->·4·->·5·->·Null⏎ Test case 2 No·of·nodes:·7 Data·for·node·1:·1 Data·for·node·2:·2 Data·for·node·3:·3 Data·for·node·4:·4 Data·for·node·5:·3 Data·for·node·6:·2 Data·for·node·7:·1 Original·linked·list:·1·->·2·->·3·->·4·->·3·->·2·->·1·->·Null⏎ Reversed·linked·list:·1·->·2·->·3·->·4·->·3·->·2·->·1·->·Null⏎
Write a C program to reverse a single linked list recursively. #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; // Write a function to create a new node with the given data // Write a function to print the linked list // Write a function to reverse the linked list recursively // Write your main function here
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.