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
47 changes: 47 additions & 0 deletions Problem1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No


// Your code here along with comments explaining your approach

/*
I use three pointers - slow, medium and fast. Slow and medium are set to null at first and fast = head. I traverse through the linked list while fast != null, I set slow = medium
and medium = fast following which fast is set to next node. I then reverse the node that medium is pointing to by setting medium.next = slow. At the end of my traversal,
my new head pointer becomes medium.
*/

/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution
{
public ListNode ReverseList(ListNode head)
{
if (head == null)
{
return head;
}

ListNode slow = null, medium = null, fast = head;

while (fast != null)
{
slow = medium;
medium = fast;
fast = fast.next;
medium.next = slow;
}

return medium;
}
}
52 changes: 52 additions & 0 deletions Problem2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No


// Your code here along with comments explaining your approach

/*
I maintain a dummy node which points to the head node and slow and fast which is equal to dummy at first. I increment fast = fast.next n number of times. Then until fast becomes the last node, I
increment both fast and slow by one node each. At the end, I set slow.next = slow.next.next (slow.next points to the node that needs to be deleted)
*/

/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution
{
public ListNode RemoveNthFromEnd(ListNode head, int n)
{
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode slow = dummy, fast = dummy;

for (int i = 0; i < n; i++)
{
fast = fast.next;
}

while (fast.next != null)
{
fast = fast.next;
slow = slow.next;
}

ListNode temp = slow.next;

slow.next = slow.next.next;

temp = null;

return dummy.next;
}
}
50 changes: 50 additions & 0 deletions Problem3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No


// Your code here along with comments explaining your approach

/*
I maintain two pointers - slow and fast which are initially pointing to the head of the linked list. I traverse through the linked list while fast!=null and fast.next!=null -
If at any point slow becomes equal to fast, I set fast to head and increment both slow and fast one by one till they coincide which would be the start of the cycle
*/

/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution
{
public ListNode DetectCycle(ListNode head)
{
ListNode slow = head, fast = head;

while (fast != null && fast.next != null)
{
slow = slow.next;
fast = fast.next.next;

if (slow == fast)
{
fast = head;
while (fast != slow)
{
slow = slow.next;
fast = fast.next;
}
return slow;
}
}

return null;
}
}