forked from dharmanshu1921/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InsertingNode.java
51 lines (47 loc) · 1.06 KB
/
InsertingNode.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package linkedlist;
public class InsertingNode {
static class LinkedList {
static Node head;
static class Node {
int data;
Node next;
Node(int d) {
data = d;
next = null;
}
}
static Node insert (Node head ,int elem,int pos) {
Node nodeToBeInserted = new Node(elem);
int count=0;
Node prev = head;
if(pos==0) {
nodeToBeInserted.next =head;
return nodeToBeInserted;
}
while(count<pos-1 && prev!=null) {
count++;
prev=prev.next;
}
if(prev!=null) {
nodeToBeInserted.next=prev.next;
prev.next=nodeToBeInserted;
}
return head;
}
static void printList(Node node) {
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.head = new Node(33);
list.head.next = new Node(90);
list.head.next.next = new Node(89);
list.head.next.next.next = new Node(7);
insert(head,777,2);
printList(head);
}
}
}