Reverse linked list c program
Write a program in C to check whether an integer is Armstrong number or not. Write a program in C to print the fibonacci series using recursive method. Write a program in C to check whether a number is palindrome or not using recursive method. C Questions on String. Write a method in C which will remove any given character from a String.
Write a program in C to count occurrence of a given character in a String. Write a program in C to check if two Strings are Anagram. Write a program in C to check a String is palindrome or not.
Leave a Comment Cancel Reply You must be logged in to post a comment. And if there are two or more elements, then we can implement an iterative solution using three-pointers. We will create a function to reverse the linked list taking reference to the head node and as the only argument and return the head of the new linked list:. Step 1: Define three nodes one with the reference to the head node and name it current, and name the other two nodes temp and prev pointers as NULL.
Step 2: Using a while loop we will traverse the linked list once until the next pointer does not become NULL. Step 3: While iterating, we perform the following operations:.
And then increment the previous node to the current node and then the current node to the temp node. O N because we iterate through each element at least once. The most important thing to remember in this approach is that the recursive approach uses a stack.
The compiler allocates stack memory after each recursive call, and this solution can run out of memory in case of very huge linked lists think billions of elements. We recursively iterate to each node in the list until we reach the last node and return the new head. We have to note that the last node in this approach will become the new head of the list. It only needs one line of code to do the linking up after current. Oh, and you'd need another line of code to track the tail node; you don't really want to be having to seek to the end of it first — Caius Jard.
Add a comment. Active Oldest Votes. Improve this answer. Caius Jard Caius Jard Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Node 85 ; list. Node 15 ; list. Node 4 ; list. WriteLine "Given linked list:" ; list. WriteLine "Reversed linked list:" ; list. Write current. Write temp. Write node. WriteLine "Original Linked list " ; list.
WriteLine "" ; Console. WriteLine "Reversed linked list " ; list.
0コメント