From 30da3c7c647095edc9e3b9d794081c3b0295d30c Mon Sep 17 00:00:00 2001 From: Anirudh Venkateshwaran Date: Sun, 17 May 2026 18:12:15 -0700 Subject: [PATCH] Solved Reverse Linked List, Remove Nth Node From End of List and Linked List Cycle II --- Problem1.cs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ Problem2.cs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Problem3.cs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 149 insertions(+) create mode 100644 Problem1.cs create mode 100644 Problem2.cs create mode 100644 Problem3.cs diff --git a/Problem1.cs b/Problem1.cs new file mode 100644 index 00000000..f3f0cec6 --- /dev/null +++ b/Problem1.cs @@ -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; + } +} \ No newline at end of file diff --git a/Problem2.cs b/Problem2.cs new file mode 100644 index 00000000..b88d0821 --- /dev/null +++ b/Problem2.cs @@ -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; + } +} \ No newline at end of file diff --git a/Problem3.cs b/Problem3.cs new file mode 100644 index 00000000..2a280bad --- /dev/null +++ b/Problem3.cs @@ -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; + } +} \ No newline at end of file