Skip to content

Commit c7c23fd

Browse files
Insertion in linked list Java (#1694)
* Create InsertionInLinkedList.java For Issue #513 * Update readme.md Co-authored-by: Tarun Yadav <[email protected]>
1 parent adbdc5f commit c7c23fd

File tree

2 files changed

+185
-8
lines changed

2 files changed

+185
-8
lines changed

Code/Java/InsertionInLinkedList.java

+181
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/*
2+
* To insert a node in a LinkedList.
3+
* There are 2 cases:
4+
* 1. To insert a node at the start of the linked list.
5+
* 2. To insert a node in between the linked list.
6+
*/
7+
8+
import java.util.*;
9+
10+
class InsertionInLinkedList
11+
{
12+
Node start;
13+
14+
InsertionInLinkedList()
15+
{
16+
start = null;
17+
}
18+
19+
void create(Node node)
20+
{
21+
//This function is to create the LinkedList
22+
if (start == null)
23+
{
24+
start = node;
25+
}
26+
else
27+
{
28+
Node temp = start;
29+
while (temp.address != null)
30+
temp = temp.address;
31+
temp.address = node;
32+
}
33+
}
34+
35+
void display()
36+
{
37+
//This function is to display the linkedlist
38+
Node temp = start;
39+
System.out.println("Linked List: ");
40+
while (temp!= null)
41+
{
42+
System.out.print(temp.data + " ");
43+
temp = temp.address;
44+
}
45+
System.out.println();
46+
}
47+
48+
void insert(int pos, int value)
49+
{
50+
//This function is to insert a node in the linkedlist
51+
Node ptr = null;
52+
53+
int ctr = 1;
54+
55+
if(pos == 1)
56+
{
57+
Node temp = new Node(value);
58+
temp.data = value;
59+
temp.address = start;
60+
start = temp;
61+
}
62+
63+
for(ptr = start; ptr != null; ptr = ptr.address)
64+
{
65+
ctr++;
66+
67+
if(ctr == pos)
68+
{
69+
Node temp = new Node(value);
70+
temp.data = value;
71+
temp.address = ptr.address;
72+
ptr.address = temp;
73+
}
74+
}
75+
}
76+
77+
public static void main()
78+
{
79+
Scanner scanner = new Scanner(System.in);
80+
81+
InsertionInLinkedList ob = new InsertionInLinkedList();
82+
83+
int data, flag=1, value, pos;
84+
while(flag!=0)
85+
{
86+
System.out.println("Enter data in linked list: ");
87+
data = scanner.nextInt(); scanner.nextLine();
88+
ob.create(new Node(data));
89+
90+
System.out.println("Do you wish to continue? Enter 1 for yes and 0 for no: ");
91+
flag = scanner.nextInt(); scanner.nextLine();
92+
}
93+
ob.display();
94+
95+
System.out.println("Enter value to be inserted in the linked list: ");
96+
value = scanner.nextInt(); scanner.nextLine();
97+
System.out.println("Enter position of insertion in the linked list: ");
98+
pos = scanner.nextInt(); scanner.nextLine();
99+
100+
ob.insert(pos, value);
101+
ob.display();
102+
}
103+
}
104+
class Node
105+
{
106+
public int data;
107+
public Node address;
108+
Node(int d)
109+
{
110+
data = d;
111+
address = null;
112+
}
113+
}
114+
115+
/*
116+
Test Cases-
117+
118+
1.
119+
Enter data in linked list:
120+
1
121+
Do you wish to continue? Enter 1 for yes and 0 for no:
122+
1
123+
Enter data in linked list:
124+
2
125+
Do you wish to continue? Enter 1 for yes and 0 for no:
126+
1
127+
Enter data in linked list:
128+
3
129+
Do you wish to continue? Enter 1 for yes and 0 for no:
130+
1
131+
Enter data in linked list:
132+
4
133+
Do you wish to continue? Enter 1 for yes and 0 for no:
134+
1
135+
Enter data in linked list:
136+
5
137+
Do you wish to continue? Enter 1 for yes and 0 for no:
138+
0
139+
Linked List:
140+
1 2 3 4 5
141+
Enter value to be inserted in the linked list:
142+
6
143+
Enter position of insertion in the linked list:
144+
3
145+
Linked List:
146+
1 2 6 3 4 5
147+
148+
2.
149+
Enter data in linked list:
150+
1
151+
Do you wish to continue? Enter 1 for yes and 0 for no:
152+
1
153+
Enter data in linked list:
154+
2
155+
Do you wish to continue? Enter 1 for yes and 0 for no:
156+
1
157+
Enter data in linked list:
158+
3
159+
Do you wish to continue? Enter 1 for yes and 0 for no:
160+
1
161+
Enter data in linked list:
162+
4
163+
Do you wish to continue? Enter 1 for yes and 0 for no:
164+
1
165+
Enter data in linked list:
166+
5
167+
Do you wish to continue? Enter 1 for yes and 0 for no:
168+
0
169+
Linked List:
170+
1 2 3 4 5
171+
Enter value to be inserted in the linked list:
172+
7
173+
Enter position of insertion in the linked list:
174+
1
175+
Linked List:
176+
7 1 2 3 4 5
177+
178+
Time Complexity: O(n)
179+
Space Complexity: O(n)
180+
where n is the number of elements in the linked list
181+
*/

Linked_list/readme.md

+4-8
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,15 @@ properties.
3131

3232
* Alternate Node Deletion ----> [C++](/Code/C++/Deletion of Alternate nodes in a linked list.cpp)
3333
* Cycle detection using Hare and Tortoise algorithm ----> [C++](/Code/C++/CycleDetectLinkedList.cpp) | [Java](/Code/Java/Cycle_Detection_In_Linked_List.java)
34+
* Insertion ----> [C++](/Code/C++/insertion_in_linked_list.cpp) | [Java](/Code/Java/InsertionInLinkedList.java)
35+
* Splitting ----> [C++](/Code/C++/Splitting_Linked_lists.cpp)
3436
* Deleting Node from Linked List ----> [Java](/Code/Java/DeleteNodeFromLinkedList.java)
3537
* Delete a given node without using head pointer ----> [C++](/Code/C++/delete_without_head.cpp)
36-
* Finding intersection node of two linked lists connected in y shape -----> [C++](Code/C++/Intersection_point_of_linked_list.cpp)
37-
* Finding Intersection of Linked Lists ----> [Python](/Code/Python/linked_lists_intersection.py)
38+
* Finding intersection node of two linked lists connected in y shape -----> [C++](Code/C++/Intersection_point_of_linked_list.cpp) | [Python](/Code/Python/linked_lists_intersection.py)
3839
* Implementation of Linked List ----> [Python](/Code/Python/linked_list.py)
39-
* Insertion ----> [C++](/Code/C++/insertion_in_linked_list.cpp)
40-
* Ispalindrome ----> [Python](/Code/Python/isPalindrome_linked_list.py)
4140
* Middle Element ----> [Python](/Code/Python/middle_element_linked_list.py)
42-
* Palindrome Check---->[C++](/Linked_list/PalindromeCheck.cpp) | [Java](/Linked_list/palindrome_checker.java)
41+
* Palindrome Check---->[C++](/Linked_list/PalindromeCheck.cpp) | [Java](/Linked_list/palindrome_checker.java) | [Python](/Code/Python/isPalindrome_linked_list.py)
4342
* Remove Duplicates from Unsorted List ----> [C++](/Code/C++/remove_duplicates.cpp)
4443
* Reversing a Linked List ----> [C++](/Code/C++/reverse_a_linked_list.cpp)
45-
* Remove Loop From Linked List C++(Linked_List/RemoveLoopFromLinkedList.cpp)
4644
* Reverse Linked List in groups of size k ---->[Python](/Code/Python/reverse_linkedlist_in_group_of_size_k.py)
47-
* Reversing a Linked List ----> [C++](/Code/C++/reverse_a_linked_list.cpp)
48-
* Splitting ----> [C++](/Code/C++/Splitting_Linked_lists.cpp)
4945
* Unrolled Linked List Implementation ----> [C++](/Code/C++/unrolled_linked_list.cpp)

0 commit comments

Comments
 (0)