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
23 changes: 23 additions & 0 deletions problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
Time complexity = O(N);
Space Complexity= O(1);
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this :
Connecting the current pointer back to the list after breaking the connection

*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode prev=null;
ListNode curr=head;
while(curr!=null){//reverse linking the nodes from the start-null and last node as head
ListNode temp=curr.next;
curr.next=prev;
prev=curr;
curr=temp;

}

return prev;
}
}
40 changes: 40 additions & 0 deletions problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No, just needed to be careful with dummy node and pointers.


// Your code here along with comments explaining your approach in three sentences only
// I use two pointers with a dummy node so edge cases like deleting head are handled easily.
// First I move the fast pointer n+1 steps ahead and then move both pointers until fast reaches null.
// Now slow will be just before the node to delete, so I skip that node and return dummy.next.

class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {

ListNode dummy = new ListNode(-1);
dummy.next = head;

ListNode slow = dummy;
ListNode fast = dummy;

int count = 0;

// move fast pointer n+1 steps ahead
while (count <= n) {
fast = fast.next;
count++;
}

// move both pointers until fast reaches end
while (fast != null) {
slow = slow.next;
fast = fast.next;
}

// delete the nth node from end
slow.next = slow.next.next;

return dummy.next;
}
}
37 changes: 37 additions & 0 deletions problem3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
Time complexity = O(N);
Space Complexity= O(1);
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this :
Initially fast pointer went out of bounds while checking the while condition but later fixed it by modifying it .
Struggled to comeup with the logic to find the head node of cycle.

*/
public class problem3 {
public ListNode detectCycle(ListNode head) {

ListNode slow=head;
ListNode fast=head;
boolean flag=false;
while(fast!=null && fast.next!=null){
//slow moves by 1x speed.
//fast moves by 2x speed.
slow=slow.next;
fast=fast.next.next;
if(slow==fast){//if both fast and slow meets then there is cycle
flag=true;
break;
}

}
if(flag==false){//if not then no cycle
return null;
}
fast=head;
while(slow!=fast){//after cycle is found moving 1x each until both thre pointers meet and return any node in the end.
slow=slow.next;
fast=fast.next;
}
return slow;
}
}