From 9464afc0e81aa0228a77a2dd2e7259bbcdd92481 Mon Sep 17 00:00:00 2001 From: sainathek Date: Tue, 27 Jan 2026 21:36:39 -0500 Subject: [PATCH] done Linked-List-1 --- problem1.java | 23 +++++++++++++++++++++++ problem2.java | 40 ++++++++++++++++++++++++++++++++++++++++ problem3.java | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 problem1.java create mode 100644 problem2.java create mode 100644 problem3.java diff --git a/problem1.java b/problem1.java new file mode 100644 index 00000000..91e39dd1 --- /dev/null +++ b/problem1.java @@ -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; + } +} \ No newline at end of file diff --git a/problem2.java b/problem2.java new file mode 100644 index 00000000..b5fb19ea --- /dev/null +++ b/problem2.java @@ -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; + } +} diff --git a/problem3.java b/problem3.java new file mode 100644 index 00000000..208c166c --- /dev/null +++ b/problem3.java @@ -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; + } +} \ No newline at end of file