-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkList_Basic.java
More file actions
154 lines (116 loc) · 3.06 KB
/
linkList_Basic.java
File metadata and controls
154 lines (116 loc) · 3.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
public class LinkedListUse {
public static LinkedListNode<Integer> insertR(LinkedListNode<Integer> head, int data, int pos){
if(pos == 0){
LinkedListNode<Integer> newNode = new LinkedListNode<Integer>(data);
newNode.next = head;
return newNode;
}
if(head == null){
return head;
}
head.next = insertR(head.next,data,pos-1);
return head;
}
public static LinkedListNode<Integer> deleteNodeRec(LinkedListNode<Integer> head, int pos) {
if(pos == 0){
return head.next;
}
if(head == null || head.next == null){
return head;
}
head.next = deleteNodeRec(head.next,pos-1);
return head;
}
public static Node<Integer> takeInput()
{
Node<Integer> head = null, tail = null;
Scanner s = new Scanner(System.in);
int data = s.nextInt();
while(data != -1){
Node<Integer> newNode = new Node<Integer>(data);
if(head == null){
head = newNode;
tail = newNode;
}else{
// Node<Integer> temp = head;
// while(temp.next != null){
// temp = temp.next;
// }
// temp.next = newNode;
tail.next = newNode;
tail = newNode; // tail = tail.next
}
data = s.nextInt();
}
return head;
}
public static void print(Node<Integer> head){
while(head != null){
System.out.print(head.data +" ");
head = head.next;
}
System.out.println();
}
public static LinkedListNode<Integer> deleteNode(LinkedListNode<Integer> head, int pos) {
if(pos == 0){
return head.next;
}
int i = 0;
LinkedListNode<Integer> temp = head;
while(i < pos - 1 && temp != null){
temp = temp.next;
i++;
}
if (temp == null || temp.next == null)
return head;
temp.next = temp.next.next;
return head;
}
public static Node<Integer> insert(Node<Integer> head, int data, int pos){
Node<Integer> newNode = new Node<Integer>(data);
if(pos == 0){
newNode.next = head;
return newNode;
}
int i = 0;
Node<Integer> temp = head;
while(i < pos - 1){
temp = temp.next;
i++;
}
newNode.next = temp.next;
temp.next = newNode;
return head;
}
public static void main(String[] args) {
Node<Integer> node1 = new Node<Integer>(10);
Node<Integer> node2 = new Node<Integer>(20);
Node<Integer> node3 = new Node<Integer>(30);
node1.next = node2;
node2.next = node3;
Node<Integer> head = node1;
print(head);
print(head);
}
private static LinkedListNode reverse(LinkedListNode<Integer> head){
LinkedListNode current = head;
LinkedListNode next = null;
LinkedListNode prev = null;
while ( current != null)
{
next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
}
}
public class Node<T> {
T data;
Node<T> next;
Node(T data){
this.data = data;
next = null;
}
}