-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeleteTheMiddleNodeOfLinkedList.java
More file actions
83 lines (63 loc) · 2.92 KB
/
DeleteTheMiddleNodeOfLinkedList.java
File metadata and controls
83 lines (63 loc) · 2.92 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
package Algorithms.LinkedListAlgos;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 21 April 2025
*/
public class DeleteTheMiddleNodeOfLinkedList {
static class ListNode {int val;ListNode next;ListNode(int x) {val = x;} ListNode(int x, ListNode next) { val = x; this.next = next;}}
public static void main(String[] args) {
System.out.println("deleteMiddleUsingSize(head) => ");
ListNode head = new ListNode(1, new ListNode(3, new ListNode(4, new ListNode(7, new ListNode(1)))));
deleteMiddleUsingSize(head);
for (ListNode trav = head; trav != null; trav = trav.next) {System.out.print(trav.val + " ");}
System.out.println("\ndeleteMiddleUsingThreePointers(head) => ");
head = new ListNode(1, new ListNode(3, new ListNode(4, new ListNode(7, new ListNode(1)))));
deleteMiddleUsingThreePointers(head);
for (ListNode trav = head; trav != null; trav = trav.next) {System.out.print(trav.val + " ");}
head = new ListNode(1, new ListNode(3, new ListNode(4, new ListNode(7, new ListNode(1)))));
System.out.println("\ndeleteMiddleUsingSlowFastPointers(head) => ");
deleteMiddleUsingSlowFastPointers(head);
for (ListNode trav = head; trav != null; trav = trav.next) {System.out.print(trav.val + " ");}
}
public static ListNode deleteMiddleUsingSize(ListNode head) {
if(head.next == null) return null;
ListNode trav = head;
int n=0;
for(; trav!=null; trav=trav.next) n++;
int mid = n/2;
for(trav=head; mid>1; trav=trav.next, mid--) {}
trav.next = trav.next.next;
return head;
}
public static ListNode deleteMiddleUsingThreePointers(ListNode head) {
if (head == null || head.next == null) return null;
ListNode slow = head, fast = head, prev = null;
while (fast != null && fast.next != null) {
prev = slow; // Keep track of the previous node
slow = slow.next; // Move slow pointer one step
fast = fast.next.next; // Move fast pointer two steps
}
prev.next = slow.next;
return head;
}
public static ListNode deleteMiddleUsingSlowFastPointers(ListNode head) {
if(head.next == null) return null;
ListNode s=head, f=head.next.next;
for(; f!=null && f.next!=null; s=s.next,f=f.next.next) {}
// or while (f != null && f.next != null) {s = s.next; f = f.next.next;}
s.next=s.next.next;
return head;
}
public static ListNode deleteMiddleUsingSlowFastPointers2(ListNode head) {
if(head == null || head.next == null) return null;
ListNode dummyHead = new ListNode(0, head);
ListNode slow = dummyHead;
ListNode fast = head;
while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
}
slow.next = slow.next.next;
return head;
}
}