Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions LL_cycle_II.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : yes
public class LL_cycle_II {
public ListNode detectCycle(ListNode head) {
ListNode slow = head;
ListNode fast = head;
boolean flag = false;
while(fast!=null && fast.next!=null){
slow = slow.next;
fast=fast.next.next;
if(slow==fast){
flag = true;
break;
}
}
fast = head;
if(!flag) return null;
while(slow!=fast){
slow=slow.next;
fast=fast.next;
}
return slow;
}
}
25 changes: 25 additions & 0 deletions remove_nth_node.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode :yes
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode slow= dummy;
ListNode fast = dummy;
int cnt=0;
while(cnt<=n){
fast = fast.next;
cnt++;
}
while(fast!=null){
slow=slow.next;
fast=fast.next;
}
ListNode temp = slow.next;
slow.next = slow.next.next;
temp.next = null;
return dummy.next; // head is no longer the orginal head if it was deleted

}
}
18 changes: 18 additions & 0 deletions reverse_ll.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Time Complexity : O(n)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode : yes
// we use 3 pointers to reverse the list by storing the values with which we loose connection
class Solution {
public ListNode reverseList(ListNode head) {
ListNode curr = head;
ListNode prev = null;
while( curr!= null){
ListNode temp = curr.next;
curr.next = prev;
prev = curr;
curr = temp;
// System.out.println(curr);
}
return prev;
}
}