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
49 changes: 49 additions & 0 deletions LinkedListcycle2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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
/*
Take slow and fast pointers to check if we have a cycle or not firstly. If we see slow and fast pointers
meeting each other, then we break the loop and update the flag to confirm cycle exists.Now, start the slow
pointer from head again and then move both one step at a time until they meet.Return the node of slow pointer.
*/
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
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;
}
}

if(!flag)
return null;

slow = head;
while(slow != fast) {
slow = slow.next;
fast = fast.next;
}
return slow;
}
}
45 changes: 45 additions & 0 deletions RemoventhNodeFromList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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
/*
Compute the overall size of the linked list and get the difference of the size and n to get the node
before the required node to be deleted. We iterate to reach that node position by decrementing the difference
variable.Now, we establish connection using next pointers and delete the required node.
*/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode curr = head;
int size = 0;

while(curr != null) {
curr = curr.next;
size++;
}

curr = head;
size = size - n;

if(size == 0)
return head.next;

while(size > 1) {
curr = curr.next;
size--;
}
curr.next = curr.next.next;
return head;
}
}
36 changes: 36 additions & 0 deletions ReverseLinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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
/*
Maintain 2 pointers previous and current to iteratively connect in a way where current points to previous.
Make sure to store current's next element in a temporary node before this connection is made. Later, swap
the previous, current and temp nodes to continue the reversing process.
*/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
//Iteratively
ListNode prev = null;
ListNode curr = head;

while(curr != null) {
ListNode temp = curr.next;
curr.next = prev;
prev = curr;
curr = temp;
}
return prev;
}
}