Question: Write a program to check if the given Linked List is a palindrome or not? Input: 1.> 4, 8, 15, 16, 23, 42 2.> 4, 8, 15, 16, 15, 8, 4 Output: 1.> IS NOT A PALINDROME 2.> IS A PALINDROME What is a Palindrome? According to Wikipedia, A palindrome is a word, phrase, number, or other sequence of symbols or elements, whose meaning may be interpreted the same way in either forward or reverse direction. Thus, numbers like 121, 1331, 98711789 are palindromes. Similarly, Linked Lists can be …
nikoo28
nikoo28
a tech-savvy guy and a design buff... I was born with the love for exploring and want to do my best to give back to the community. I also love taking photos with my phone to capture moments in my life. It's my pleasure to have you here.
-
-
Question: Given a Linked List, we need to find the middle of the list and print the number. Input: 4, 8, 15, 16, 23, 42, 99 Output: 16 This is a very basic question and is a part of a bigger problem. However, there are several approaches to solve the above problem. BRUTE-FORCE APPROACH In this approach, we count the number of nodes in the list for each of the node and see whether it is the middle. Time Complexity: O(n2) Space Complexity: O(1) MORE SIMPLIFIED APPROACH In this approach, …
-
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 …
-
Question: Given a Linked List, we need to find out if the number of nodes in the Link List are odd or even? Input: 4, 8, 15, 16, 23, 42 Output: EVEN The most easy method to solve this problem would be by traversing the entire Linked List and counting the number of nodes as we go. As the loop is finished, we can check if the count is even or odd. A Simpler Way: Another way of solving this problem in less time would be advancing 2 nodes at …
-
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 …
-
Question: How will you reverse a singly Linked List? Input:– 4 -> 8 -> 15 -> 16 -> 23 -> 42 Output:– 42 -> 23 -> 16 -> 15 -> 8 -> 4 Reversing a singly Linked List is based upon a very simple concept. We just need to swap the “NEXT” value of each of the node, as we iterate over the list. That means that after the complete process, the first node should point to NULL, and the second last node should point to the first node…and so …
-
Question: In C you can do something like:- char s[] = “Hello World”; and char *s = “Hello World”; What is the difference between the two of them? What happens to the memory during compile time and run-time? This declaration: Creates ONE object – a char array of size 6, called s, initialized with the values ‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’. Where this array is allocated in memory, and how long it lives for, depends on where the declaration appears. If the declaration is within a function, it will …
-
Question: C does not have the boolean data type by default. Is there a way by which we can implement it? In C language, we do not have the boolean datatype by default. That means we cannot do something like We can do this in C++ and JAVA. However, we have an alternative method to achieve the same functionality. Before moving on to the implementation it is important to know that in C, if a value is ‘0 (zero)’ it is treated as false, and any other value other than …
-
Question: Write a program to insert a node in a given sorted Linked List. Given List: 23 -> 32 -> 99 -> 101 -> 2222 Node to add:- 50 Output:- 23 -> 32 -> 50 -> 99 -> 101 -> 2222 To insert a node in a sorted Linked List, we need to perform a basic Linked List operation discussed in this post. Insert a node in a Linked List. In the given post, we knew the position at which we had to insert the node. But in this case …
-
Question: What is the difference between doing: ptr = (char **) malloc (MAXELEMS * sizeof(char *)); or: ptr = (char **) calloc (MAXELEMS, sizeof(char*)); When is it a good idea to use calloc and when to use malloc ? There are two differences:- Firstly it is in the number of arguments, malloc() takes a single argument (memory required in bytes), while calloc() needs two arguments. Secondly, malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO. calloc() allocates a memory area, the length …