diff --git a/LinkedListCycle.cpp b/LinkedListCycle.cpp new file mode 100644 index 00000000..12694871 --- /dev/null +++ b/LinkedListCycle.cpp @@ -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) diff --git a/removeNtheNodefromEnd.cpp b/removeNtheNodefromEnd.cpp new file mode 100644 index 00000000..456de064 --- /dev/null +++ b/removeNtheNodefromEnd.cpp @@ -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) \ No newline at end of file diff --git a/reverseLinkedList.cpp b/reverseLinkedList.cpp new file mode 100644 index 00000000..9431c638 --- /dev/null +++ b/reverseLinkedList.cpp @@ -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) \ No newline at end of file