-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinsert_node.java
61 lines (46 loc) · 1.49 KB
/
insert_node.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
52
53
54
55
56
57
58
59
60
61
package Singly_Linked_List;
/*
Given a SLL, a pos & a data item, insert data-item at given pos(1-based indexing)
ip: 10->30->50->70, pos=2, data=20
op: 10->20->30->50->70
ip: 5->15->25->35, pos=5, data=10
op: 5->15->25->35->10
ip: 10->20, pos=4, data=5
op: 10->20
idea: we need to traverse the list (pos-2) times, so we can reach (pos-1)th node, let's call it curr node, we will insert a newNode after it
after that we need to point newNode's next to where curr's next is pointing & change curr's next to newNode
*/
public class insert_node {
public static void main(String[] args) {
Node head = null;
// head = insertAtPos(head, 20);
// head = insertEnd(head, 10);
printList(head);
}
public static Node insertAtPos(Node head, int data, int pos) {
Node newNode = new Node(data);
if(pos==1)
{
newNode.next = head;
return newNode;
}
Node prevNode = head;
// go to pos-1th node
for(int i = 1; i<=pos-2 && prevNode!=null; i++)
prevNode = prevNode.next;
// case: pos > LL size
if(prevNode==null)
return head;
// actual logic
newNode.next = prevNode.next;
prevNode.next = newNode;
return head;
}
public static void printList(Node head) {
Node curr = head;
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.next;
}
}
}