Java program to insert a new node at the beginning of the singly linked list17 Mar 2025 | 3 min read In this program, we will create a singly linked list and add a new node at the beginning of the list. To accomplish this task, we will store head to a temporary node temp. Make newly added node as the new head of the list. Then, add temp (old head) after new head. ![]() Consider the above list; node 1 represents the head of the original list. Let node New be the new node which needs to be added at the beginning of the list. Node temp will point to head, i.e., 1. Make New as the new head of the list and add temp after new head such that node next to New will be 1. Algorithm
a. display() will display the nodes present in the list:
Program:Output: Adding nodes to the start of the list: 1 Adding nodes to the start of the list: 2 1 Adding nodes to the start of the list: 3 2 1 Adding nodes to the start of the list: 4 3 2 1 Next TopicJava Programs |
In this section, we will learn what is a spy number and also create Java programs to check if the given number is Spy or not. The spy number program is frequently asked in Java coding test. Spy Number A positive integer is called a spy number if...
3 min read
In this program, we need to find the most repeated word present in given text file. This can be done by opening a file in read mode using file pointer. Read the file line by line. Split a line at a time and store in...
2 min read
Java Convert String to Object We can convert String to Object in java with assignment operator. Each class is internally a child class of Object class. So you can assign string to Object directly. You can also convert String to Class type object using Class.forName() method. Java String to...
1 min read
In this program, we need to calculate the difference between the sum of nodes present at odd levels and sum of nodes present at even levels. Suppose, if a tree contains 5 levels, then Difference = (L1 + L 3 + L5) - (L2 + L4) In...
8 min read
. In this program, we need to count each character present in the string and find out the maximum and minimum occurring character. Grass is greener on the other side In above example, character 'a' is occurred only once in the string. So, it is minimum occurring character...
5 min read
12344321 123**321 12****21 1******1 Algorithm: STEP 1: START STEP 2: SET lines=4 STEP 3: DEFINE i, j. STEP 4: SET space=0 STEP 5: SET i=0 STEP 6: REPEAT STEP 7 TO 20 UNTIL i STEP 7: SET j=1 STEP 8: REPEAT STEP 9 UNTIL j <= space. STEP 9: PRINT " " and SET J=J+1 STEP 10: SET...
2 min read
Java Convert Decimal to Binary We can convert decimal to binary in java using Integer.toBinaryString() method or custom logic. Java Decimal to Binary conversion: Integer.toBinaryString() The Integer.toBinaryString() method converts decimal to binary string. The signature of toBinaryString() method is given below: public static String toBinaryString(int decimal) Let's see the simple example...
1 min read
It is a problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are going to solve...
4 min read
In this program, we need to create the binary tree by inserting nodes and displaying nodes in in-order fashion. A typical binary tree can be represented as follows: In the binary tree, each node can have at most two children. Each node can have zero, one...
4 min read
Java String to long We can convert String to long in java using Long.parseLong() method. Scenario It is generally used if we have to perform mathematical operations on the string that contains long number. Whenever we get data from textfield or textarea, entered data is received as a string....
1 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India