-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.java
More file actions
189 lines (150 loc) · 4.54 KB
/
Node.java
File metadata and controls
189 lines (150 loc) · 4.54 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
public class Node {
int val;
Node next;
public Node(int val) {
this.val = val;
this.next = null;
}
// (1) Return the length of the linked list
public static int getLength(Node head) {
int length = 0;
Node temp = head;
while(temp != null) {
temp = temp.next;
length++;
}
return length;
}
// (2) Print LL
public static void print(Node head) {
Node temp = head;
while(temp != null) {
System.out.print(temp.val + " -> ");
temp = temp.next;
}
}
// (3) Insert a node at the start
// TC: O(1)
public static Node insertAtStart(Node head, int val) {
Node NewNode = new Node(val);
NewNode.next = head;
head = NewNode;
return head;
}
// (4) Delete a Node at the start
public static Node deleteFromStart(Node head) {
if(head == null) return null;
return head.next;
}
// (5) Given a LL. Return the mid node.
// TC: O(N) SC: O(1)
public static Node midNode(Node head) {
Node slow = head, fast = head;
while(fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
// (6) Given a LL. Reverse it.
// TC: O(N) SC: O(1)
public static Node reverseList(Node head) {
Node h2 = null, curr = head;
while(curr != null) {
Node next = curr.next;
curr.next = h2;
h2 = curr;
curr = next;
}
return h2;
}
// (7) Given a LL. Reverse the first k nodes of the LL.
public static Node reverse_k(Node head, int k) {
Node h2 = null, curr = head, next = curr.next;
while(curr != null && k > 0) {
next = curr.next;
curr.next = h2;
h2 = curr;
curr = next;
k--;
}
if(head != null) head.next = curr;
return h2;
}
// (8) Given 2 sorted LL. Merge them into a single sorted LL.
// TC: O(N+M) SC: O(1)
public static Node mergeSortedLL(Node h1, Node h2) {
if(h1 == null) return h2;
if(h2 == null) return h1;
Node ans = null;
if(h1.val < h2.val) {
ans = h1;
h1 = h1.next;
} else {
ans = h2;
h2 = h2.next;
}
Node temp = ans;
while(h1 != null && h2 != null) {
if(h1.val < h2.val) {
temp.next = h1;
temp = h1;
h1 = h1.next;
} else {
temp.next = h2;
temp = h2;
h2 = h2.next;
}
}
if(h1 != null) temp.next = h1;
if(h2 != null) temp.next = h2;
return ans;
}
// (9) MERGE SORT IN LL
public static Node getMid(Node head) {
Node slow = head, fast = head.next;
while(fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
public static Node mergeSort(Node head) {
if(head == null || head.next == null) return head;
Node m = getMid(head);
Node head2 = m.next;
m.next = null;
head = mergeSort(head);
head2 = mergeSort(head2);
return mergeSortedLL(head, head2);
}
// (10) Detect if there is a cycle in the LL.
// TC: O(N) SC: O(1)
public static boolean checkCycle(Node head) {
Node slow = head, fast = head;
while(fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if(slow == fast) {
return true;
}
}
return false;
}
// (11) Given a node of LL. Delete this node.
public static void deleteNode(Node node) {
node.val = node.next.val;
node.next = node.next.next;
}
public static void main(String[] args) {
// Node head = new Node(3);
// head.next = new Node(12);
// head.next.next = new Node(10);
Node n1 = new Node(1);
n1.next = new Node(2);
n1.next.next = new Node(3);
n1.next.next.next = new Node(2);
n1.next.next.next.next = new Node(1);
print(n1);
}
}