Skip to content

Commit

Permalink
Merge pull request #240 from Neha1204/master
Browse files Browse the repository at this point in the history
Create Detect loop in LinkedList.cpp
  • Loading branch information
sat5297 authored Oct 16, 2020
2 parents 672cffe + 9914c02 commit 5d99918
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Detect loop in LinkedList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {

if(head== NULL || head->next == NULL) return false;

ListNode *slow= head->next, *fast= slow->next;

while(slow!= NULL && fast != NULL && slow!= fast){
slow= slow->next;
fast= fast->next;
if(fast!=NULL) fast=fast->next;
}

if(slow==NULL || fast ==NULL) return false;

return true;

}
};
13 changes: 13 additions & 0 deletions Numeric string to int
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
int StringToInt(string s){
int ans = 0, i=0;

if(s.size()==0) return 0;
if(s[0] == '-') i++;

for( i; i<s.size(); i++){
ans = ans*10 + (s[i]-48);
}

if(s[0] == '-') ans = ans*(-1);
return ans;
}

0 comments on commit 5d99918

Please sign in to comment.