-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListBasic
More file actions
115 lines (102 loc) · 2.06 KB
/
Copy pathLinkedListBasic
File metadata and controls
115 lines (102 loc) · 2.06 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package Tuf;
class Node{
int data;
Node next;
Node(int data1,Node next1){
this.data = data1;
this.next = next1;
}
Node(int data1){
this.data = data1;
}
}
public class ll_basic {
private static Node Arr_to_ll(int[] arr) { //Node because we are returning pointer to the head
Node head = new Node(arr[0]);
Node mover = head;
for(int i=1;i<arr.length;i++) {
Node temp = new Node (arr[i]);
mover.next = temp; // connecting
mover = temp;
}
return head;
}
private static int findnum(Node head, int num) {
Node temp = head;
while(temp != null) {
if(temp.data == num) {
return temp.data;
}
temp = temp.next;
}
return 0;
}
private static Node removehead(Node head) {
if(head == null) return head;
head = head.next;
return head;
}
private static Node removetail(Node head) {
Node mover = head;
while(mover.next.next != null) {
mover = mover.next;
}
mover.next = null;
return head;
}
private static Node removeany(Node head,int num) {
int ctr = 0;
Node temp = head;
Node prev = null;
while(temp != null) {
ctr++;
if(ctr == num) {
prev.next = prev.next.next;
break;
}
prev = temp;
temp = temp.next;
}
return head;
}
private static Node insertop(Node head, int num) {
Node a = new Node(num,head);
return a;
}
private static Node insertlast(Node head,int num) {
Node temp = head;
Node x = new Node(num);
while(temp.next!=null) {
temp = temp.next;
}
temp.next = x;
return head;
}
private static Node insertbw(Node head, int el,int num) {
int ctr = 0;
Node temp = head;
while(temp != null) {
ctr++;
if(ctr == num) {
Node x = new Node(el,temp.next);
temp.next = x;
break;
}
temp = temp.next;
}
return head;
}
private static void print(Node head) {
while(head != null) {
System.out.print(head.data + " ");
head = head.next;
}
System.out.println();
}
public static void main(String[] args) {
int[] arr = {1,2,3,4};
Node head = Arr_to_ll(arr);
head = insertbw(head,7,3);
print(head);
}
}