-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday27.java
More file actions
57 lines (51 loc) · 1.16 KB
/
day27.java
File metadata and controls
57 lines (51 loc) · 1.16 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
Q1: https://bit.ly/3w6hUaa
// PROBlem:-reverse the doubly linked list
class Solution {
public DLLNode reverseDLL(DLLNode head) {
// Your code here
DLLNode curr=head;
DLLNode after=null;
DLLNode early=null;
while(curr!=null){
after=curr.next;
curr.next=early;
early=curr;
curr=after;
}
return early;
}
}
TC-O(N)
SC-O(1)
Q2: https://bit.ly/3QlEoMx
PROBLEM:-deletion in doubly linked list
class Solution {
public Node deleteNode(Node head, int x) {
if (head == null) {
return null;
}
if (x == 1) {
head = head.next;
if (head != null) {
head.prev = null;
}
return head;
}
Node curr = head;
int i = 1;
while (i < x - 1 && curr != null) {
curr = curr.next;
i++;
}
if (curr == null || curr.next == null) {
return head;
}
curr.next = curr.next.next;
if (curr.next != null) {
curr.next.prev = curr;
}
return head;
}
}
TC-O(N)
SC-O(1)