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

LinkedList 1 completed #1490

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


// Your code here along with comments explaining your approach
class Problem1 {
public ListNode reverseList(ListNode head) {
if(head==null) return head;
//return reverseListItr(head);
ListNode newHead = reverseListRecur(head);
head.next = null;
return newHead;

}
//TC :O(N)
//SC :O(1)
public ListNode reverseListItr(ListNode head) {

ListNode prev = null;
ListNode curr = head;
ListNode temp = null;

while(curr != null){

temp=curr.next;
curr.next = prev;
prev = curr;
curr = temp;
}


return prev;
}
//TC :O(N)
//SC :O(N)
public ListNode reverseListRecur(ListNode node) {

if(node.next == null){
return node;
}
ListNode nextNode = reverseListRecur(node.next);
node.next.next = node;

return nextNode;
}
}
42 changes: 42 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Time Complexity :
// Space Complexity :
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this :


// Your code here along with comments explaining your approach

/**
* 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 Problem2 {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode fast = dummy;
ListNode slow = dummy;
int count = 0;

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

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

slow.next = slow.next.next;

return dummy.next;
}

}
51 changes: 51 additions & 0 deletions Problem3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Time Complexity :
// Space Complexity :
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this :


// Your code here along with comments explaining your approach
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Probem3 {
public ListNode detectCycle(ListNode head) {
if(head == null || head.next == null ){
return null;
}
ListNode slow = head;
ListNode fast = head;


while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) { // Cycle detected
break;
}
}

if(slow == fast){
slow = head;

while(slow != fast){

slow = slow.next;
fast = fast.next;
}
}else{
return null;
}


return fast;
}
}