Skip to content

Commit 9df2c0c

Browse files
authored
Linked_list insertion and creation
1 parent b0fe29d commit 9df2c0c

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

linked_list/.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// CREATION AND INSERTION
2+
3+
import java.io.*;
4+
public class LinkedList {
5+
Node head;
6+
7+
static class Node {
8+
9+
int data;
10+
Node next;
11+
12+
// Constructor
13+
Node(int d)
14+
{
15+
data = d;
16+
next = null;
17+
}
18+
}
19+
20+
public static LinkedList insert(LinkedList list, int data)
21+
{
22+
Node new_node = new Node(data);
23+
24+
if (list.head == null) {
25+
list.head = new_node;
26+
}
27+
else {
28+
29+
Node last = list.head;
30+
while (last.next != null) {
31+
last = last.next;
32+
}
33+
34+
last.next = new_node;
35+
}
36+
37+
return list;
38+
}
39+
40+
public static void printList(LinkedList list)
41+
{
42+
Node currNode = list.head;
43+
44+
System.out.print("LinkedList: ");
45+
46+
while (currNode != null) {
47+
48+
System.out.print(currNode.data + " ");
49+
50+
currNode = currNode.next;
51+
}
52+
}
53+
54+
public static void main(String[] args)
55+
{
56+
57+
LinkedList list = new LinkedList();
58+
59+
60+
list = insert(list, 0);
61+
list = insert(list, 9);
62+
list = insert(list, 8);
63+
list = insert(list, 7);
64+
list = insert(list, 6);
65+
list = insert(list, 5);
66+
list = insert(list, 4);
67+
list = insert(list, 3);
68+
69+
printList(list);
70+
}
71+
}

0 commit comments

Comments
 (0)