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

Linked-List-1 Complete #1501

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
39 changes: 39 additions & 0 deletions LinkedListCycle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
//initialize two pointer fast and slow
ListNode *slow = head;
ListNode *fast = head;

// now to detect the cycle
while(fast && fast->next)
{
slow = slow->next;
fast = fast->next->next;
if(fast == slow)
break;
}
//check if no cycle is detected
if(!fast || !fast->next)
return NULL;
//once we detected the cycle
slow = head;
while(slow != fast)
{
slow= slow->next;
fast= fast->next;
}

return slow;
}
};
//Time Complexity O(N)
//Space Complexity O(1)
47 changes: 47 additions & 0 deletions removeNtheNodefromEnd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
// to remove the the nth node from the end
// we need two pointer
// first = head
// second = head +n;
// now we iterate both the pointer untill the second pointer.next is null cause we want the
// node previous then nth node then we delete the node

class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* dummy = new ListNode(0);
dummy->next = head;
ListNode* first = dummy;
ListNode* second = dummy;

while(n>=0)
{
second = second->next;
n--;
}

while(second != NULL)
{
first = first ->next;
second = second->next;
}
//delete the node
ListNode *tmp = first->next->next;
first->next = first->next->next;
tmp = NULL;

return dummy->next;
}
};

//Time complexity O(N)
//space complexity O(1)
44 changes: 44 additions & 0 deletions reverseLinkedList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/

// we can reverse the linked by intalizing three pointer
// curr = head
// prev = -1//dummynode
// temp = head.next

// now by keeping track of the next node we can update the pointer of current
// curr.next = prev
// prev = curr
// curr =tmp
// tmp = tmp.next

class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head == NULL)
return head;
ListNode *curr = head;
ListNode *prev = NULL;
ListNode *temp = curr->next;

while(temp != NULL)
{
curr->next = prev;
prev = curr;
curr = temp;
temp = temp->next;
}
curr->next = prev;
return curr;
}
};
//Time complexity O(N)
//Space complexity O(1)