Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed LinkedList-1 #1505

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
41 changes: 41 additions & 0 deletions DetectCycle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Time Complexity :O(n) (n amount and c is umber of coins )
// Space Complexity :0(1)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : nothing

import java.util.LinkedList;
import java.util.Queue;

public class DetectCycle {
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; }
}

public ListNode detectCycle(ListNode head) {
ListNode slow = head;
ListNode fast = head;
boolean flag =true;

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

if(slow == fast){
flag = false;
break;
}
}
if(flag) return null;
slow = head;
while(slow != fast){
slow = slow.next;
fast= fast.next;
}

return slow;
}
}
38 changes: 38 additions & 0 deletions RemoveNthNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Time Complexity :O(n)
// Space Complexity :0(1)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : nothing



public class RemoveNthNode {
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; }
}

public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(-1);
dummy.next=head;
int count =0; ListNode slow = dummy; ListNode fast=dummy;

while(count<=n){
fast = fast.next;
count++;
}

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

ListNode temp = slow.next;
slow.next = slow.next.next;
temp = null;

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

public class ReverseLinkedList {
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; }
}

public ListNode reverseList(ListNode head) {
if(head == null){
return null;
}

ListNode prev = null;
ListNode curr = head;

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

return prev;
}
}