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 done #1484

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
50 changes: 50 additions & 0 deletions RemoveNthNodeFromEndofList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// brute force - traverse first for the length
//traverse second for length - n then delete node TC- O(2n)

/**
* 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; }
* }
*/

// with TC - O(n) take two pointers

class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {

if(head == null) return null;
int count = 0;

ListNode dummy = new ListNode(-1);//we need dummy because if we need to remove head in that case.

dummy.next = head;

ListNode slow = dummy;
ListNode fast = dummy;

while(count<=n){

//if (fast == null) return null; // In case n is larger than the list length
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;
}
}
84 changes: 84 additions & 0 deletions ReverseLinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//TC- O(n) SC-O(1)
//if we want to use extra space put linkedlist in array then from end of array put it back in linkedlist

/**
* 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) {

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

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


//reverse linkedlist using recurssion, here we do not need to pass pointers, when recurssion will come it will have
// all the data, with recurssion we will use extra stack space, everytime recurssion function go back to its
// call it will return last node; such as 1>2>3>4>5 when head.next ==null return will be top function on stack
//that is f(5) ; now head is next function in stack 4 this is head, now we reverse.
//TC- O(n) SC- O(n)

// class Solution {
// public ListNode reverseList(ListNode head) {

// if(head == null || head.next == null) return head;

// ListNode result = reverseList(head.next);

// head.next.next = head;

// head.next = null;

// return result;


// }
// }

//recurssion with void
class Solution {
ListNode result;

public ListNode reverseList(ListNode head) {

if(head == null) return null;

helper(head);

return result;


}

private void helper(ListNode head){
if(head.next == null){
result = head;
return;

}
reverseList(head.next);
head.next.next = head;
head.next = null;



}
}

114 changes: 114 additions & 0 deletions Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
//find length of cycle, move s ahead by length of the cycle times, move s and f one by one it will meet at start. s n f are two simple pointer
// public class Solution {
// public ListNode detectCycle(ListNode head) {
// int length = 0;

// ListNode fast = head;
// ListNode slow = head;

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

// if (length == 0) {
// return null;
// }

// // find the start node
// ListNode f = head;
// ListNode s = head;

// while (length > 0) {
// s = s.next;
// length--;
// }

// // keep moving both forward and they will meet at cycle start
// while (f != s) {
// f = f.next;
// s = s.next;
// }
// return s;
// }

// // find length of the cycle
// public int lengthCycle(ListNode head) {
// ListNode fast = head;
// ListNode slow = head;

// while (fast != null && fast.next != null) {
// fast = fast.next.next;
// slow = slow.next;
// if (fast == slow) {
// // calculate the length
// ListNode temp = slow;
// int length = 0;
// do {
// temp = temp.next;
// length++;
// } while (temp != slow);
// return length;
// }
// }
// return 0;
// }
// }

//to find the cycle we need 2x and 1x speed 2 pointers . with 3x or 4x will take more time to detect cycle
//and only 2x will be able to give head of cycle

//2 loops first to find the meting point, move f with 2x and s with 1x where they same their meeting point.
//second loop to find the head of cycle, that is suppose a is distace from head of linkedlist to head of cycle,
// b is dis from head of cycle to meeting point, and c is dis from meeting point to head of cycle. so in this case
//for slow and fast pointer a+b = 1/2(a+b+c+b) i.e. a = c, so where a=c that is head, meaning we will move fast
// from meeting point one step and head of linkedlist one step where they meet that is head of cycle.

//TC-O(2n) SC-O(1)
public class Solution {
public ListNode detectCycle(ListNode head) {

if(head == null) return null;
boolean flag = false;

ListNode slow = head;
ListNode fast = head;
// checking for cycle
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;


}
}