id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "int dp[1001][1001];\nint solve(string &s,int i,int j){\n if(i==j){\n return 0;\n }\n\n if(i>j){\n return 0;\n }\n if(dp[i][j]!=-1){\n return dp[i][j];\n }\n int ans=0;\n if(s[i]==s[j]){\n ans=solve(s,i+1,j-1);\n }else{\n ans=1+min(solve(s,i+1,j),solve(s,i,j-1));\n }\n dp[i][j]=ans;\n return ans;\n}\n\nclass Solution {\npublic:\n int minInsertions(string s) {\n memset(dp,-1,sizeof(dp));\n return solve(s,0,s.size()-1);\n }\n};",
"memory": "11900"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "int t[1001][1001];\nclass Solution {\npublic:\n int lcs(string s,string s1,int n,int m){\n for(int i=0;i<=n;i++){\n for(int j=0;j<=m;j++){\n if(i==0||j==0)\n t[i][j]=0;\n }\n }\n for(int i=1;i<=n;i++){\n for(int j=1;j<=m;j++){\n if(s1[i-1]==s[j-1]){\n t[i][j]=1+t[i-1][j-1];\n }\n else t[i][j]=max(t[i-1][j],t[i][j-1]);\n }\n }\n return t[n][m];\n }\n \n \n int minInsertions(string s) {\n int n=s.length();\n memset(t,-1,sizeof(t));\n string s1=s;\n reverse(s.begin(),s.begin()+n);\n\n //cout<<lcs(s1,s,n,n);\n return (n-lcs(s1,s,n,n));\n\n }\n};",
"memory": "12000"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int LCS(string X, string Y){\n int m=X.length();\n int t[1001][1001] = {0};\n for(int i=1;i<=m;i++){\n for(int j=1;j<=m;j++){\n if(X[i-1] == Y[j-1]) t[i][j] = 1+ t[i-1][j-1];\n else t[i][j] = max(t[i-1][j] , t[i][j-1]);\n }\n }\n return t[m][m];\n }\n\n int minInsertions(string s) {\n string s1,s2;\n // doing 2 times reversal so that original string remains same\n s1 = s;\n reverse(s1.begin(),s1.end());\n s2 = s1; // reversed string\n reverse(s1.begin(),s1.end());\n\n return s.length() - LCS(s1,s2);\n }\n};",
"memory": "12100"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int t[1001][1001];\n int solve2(int i, int j, string& s)\n {\n if (i>j) return 0;\n\n if (i==j) return 1;\n\n if (t[i][j] != -1) return t[i][j];\n if(s[i] == s[j]) return t[i][j] = (2+solve2(i+1, j-1, s));\n\n return t[i][j] = max(solve2(i+1, j, s), solve2(i,j-1,s));\n }\n \n int minInsertions(string s) {\n memset(t, -1, sizeof(t));\n int a = solve2(0, s.length()-1, s);\n // cout<<a<<\"\\n\";\n return s.length() - a;\n }\n};",
"memory": "12100"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minInsertions(string s) {\n int n = s.size();\n string s2;\n for(int i=n-1;i>=0;i--){\n s2 = s2 + s[i];\n }\n vector<int>prev(n+1,0);\n vector<int>curr(n+1,0);\n for(int i=1;i<=n;i++){\n for(int j=1;j<=n;j++){\n if(s[i-1] == s2[j-1]){\n curr[j] =1+ prev[j-1];\n }\n else{\n curr[j] = max(prev[j],curr[j-1]);\n }\n }\n prev = curr;\n }\n\n\n return n-prev[n];\n }\n};",
"memory": "12200"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\nint dp[1001][1001];\nint m;\n// int solve(string s1, string s2,int i,int j){\n// if(i==m || j==m) return 0;\n// if(dp[i][j]!=-1) return dp[i][j];\n// if(s1[i]==s2[j]) return dp[i][j]=1+solve(s1,s2,i+1,j+1);\n// else{\n// return dp[i][j]=0+max(solve(s1,s2,i+1,j),solve(s1,s2,i,j+1));\n// } \n// }\nint solve(string &s, int i, int j){\n if(i>j) return 0;//i aage gya j s stop\n if(i==j) return 1;// jb dono same pmaajaye to ek ele to palindrome hota hi h\n if(dp[i][j]!=-1) return dp[i][j];\n\nif(s[i]==s[j]) return dp[i][j]=2+solve(s,i+1,j-1);//equal h to 2 ele hogya palindrome to add 2 and call for next\nelse {\n return dp[i][j]=max(solve(s,i+1,j),solve(s,i,j-1));\n //alg h i and j vla element to dono ko brha and kam krke try and add the max\n}\n}\n int longestPalindromeSubseq(string s) {\n //atoz r1\n //codewithmik\n memset(dp,-1,sizeof(dp));\n // string s2=s;\n // reverse(s2.begin(),s2.end());\n \n // m=s.length();\n //return solve(s,s2,0,0); //lcs approch\n\n return solve(s,0,s.length()-1);//2 point with dp\n }\n int minInsertions(string s) {\n //atoz r1\n //all code same as longest palindrome, just - it from total length\n return s.size()-longestPalindromeSubseq(s);\n }\n};",
"memory": "12300"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minInsertions(string s) {\n string t=\"\";\n int dp[s.size()+1][s.size()+1];\n for(int i=0;i<s.size();i++){\n t=t+s[s.size()-1-i];\n dp[0][i]=0;\n dp[i][0]=0;\n }dp[0][s.size()]=0;\n dp[s.size()][0]=0;\n for(int i=1;i<=s.size();i++){\n for(int j=1;j<=s.size();j++){\n if(s[i-1]==t[j-1]){\n dp[i][j]=1+dp[i-1][j-1];\n }else{\n dp[i][j]=max(dp[i-1][j],dp[i][j-1]);\n }\n }\n }return s.size()-dp[s.size()][s.size()];\n }\n};",
"memory": "13000"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "// using space optimisation \n\nclass Solution {\npublic:\n int minInsertions(string s) {\n string rev = \"\" ; \n int n = s.length(); \n for(auto it : s){\n rev = it + rev ; \n }\n\n int dp[n+1][n+1] ;\n for(int i=0 ; i<=n ; i++)\n for(int j=0 ; j<=n ; j++)\n dp[i][j] = 0 ; \n // base case is set and we are ready to go \n for(int i=1 ; i<=n ; i++){\n for(int j=1 ; j<=n ; j++){\n if(s[i-1] == rev[j-1]){\n dp[i][j] = dp[i-1][j-1] +1 ; \n }\n else{\n dp[i][j] = max(dp[i-1][j] , dp[i][j-1]) ; \n }\n }\n }\n //cout << dp[n] << endl; \n // for(int i=0 ; i<=n ; i++){\n // for(int j=0 ; j<=n ; j++)\n // cout << dp << \" \" ; \n // cout << endl; \n // }\n return (n - dp[n][n]) ; \n }\n};",
"memory": "13100"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int t[501][501];\n int solve(string& s, string& rev, int n, int m) {\n if (n == 0 || m == 0) {\n return 0;\n }\n if(t[n][m] != -1){\n return t[n][m];\n }\n if (s[n - 1] == rev[m - 1]) {\n return t[n][m] = 1 + solve(s, rev, n - 1, m - 1);\n }\n return t[n][m] = max(solve(s, rev, n - 1, m), solve(s, rev, n, m - 1));\n }\n int minInsertions(string s) {\n int n = s.length();\n string rev = \"\";\n for (int i = n - 1; i >= 0; i--) {\n rev = rev + s[i];\n }\n memset(t,-1,sizeof(t));\n int m = rev.length();\n int lcs = solve(s, rev, n, m);\n return n - lcs;\n }\n};",
"memory": "13200"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int dp[1001][1001];\n int f(int i, int j, string& s){\n if(i == j) return 1;\n if(i > j) return 0;\n\n if(dp[i][j] != -1) return dp[i][j];\n\n int take = INT_MIN;\n if(s[i] == s[j]){\n take = 2 + f(i+1,j-1,s);\n }\n int skipi = f(i+1,j,s);\n int skipj = f(i,j-1,s);\n return dp[i][j] = max({take,skipi,skipj});\n }\n int lps(string s) {\n int n = s.size();\n memset(dp,-1,sizeof(dp));\n return f(0,n-1,s);\n }\n\n int minInsertions(string s) {\n int ans = lps(s);\n return s.length() - ans;\n }\n};",
"memory": "13500"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "\n\nclass Solution {\npublic:\n int minInsertions(string s) {\n // Idea :\n // Divide the string in two halves, check LCS.\n\n int ns = s.size();\n\n // string s1 = s.substr(0, ns/2);\n string s2 = s;\n\n reverse(s2.begin(), s2.end());\n\n // cout << s1 << \" \" << s2;\n // LIS begins\n // int m = s1.size();\n int n = s2.size();\n\n unordered_map<char, vector<int>> map;\n\n for(int i=0; i<n; i++){\n map[s2[i]].push_back(i);\n }\n\n vector<int> seq;\n\n for(auto c : s){\n if(map.find(c) != map.end()){\n for(auto it = map[c].rbegin(); it != map[c].rend(); it++){\n seq.push_back(*it);\n }\n }\n }\n\n int lislen = LISlen(seq);\n // cout << seq.size();\n if(seq.size() == 0){\n return 0;\n }\n \n // for(int p = 0; p<seq.size(); p++){\n // cout << p << endl;\n // }\n\n int insertions = ns-lislen;\n\n return insertions;\n\n\n }\n\nprivate:\n int LISlen(const vector<int> seq){\n vector<int> lis;\n\n for(int i : seq){\n auto it = lower_bound(lis.begin(), lis.end(), i);\n\n if(it == lis.end()){\n lis.push_back(i);\n }\n else{\n *it = i;\n }\n }\n\n return lis.size();\n }\n};",
"memory": "14500"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "\n\nclass Solution {\npublic:\n int minInsertions(string s) {\n // Idea :\n // Divide the string in two halves, check LCS.\n\n int ns = s.size();\n\n // string s1 = s.substr(0, ns/2);\n string s2 = s;\n\n reverse(s2.begin(), s2.end());\n\n // cout << s1 << \" \" << s2;\n // LIS begins\n // int m = s1.size();\n int n = s2.size();\n\n unordered_map<char, vector<int>> map;\n\n for(int i=0; i<n; i++){\n map[s2[i]].push_back(i);\n }\n\n vector<int> seq;\n\n for(auto c : s){\n if(map.find(c) != map.end()){\n for(auto it = map[c].rbegin(); it != map[c].rend(); it++){\n seq.push_back(*it);\n }\n }\n }\n\n int lislen = LISlen(seq);\n // cout << seq.size();\n if(seq.size() == 0){\n return 0;\n }\n \n // for(int p = 0; p<seq.size(); p++){\n // cout << p << endl;\n // }\n\n int insertions = ns-lislen;\n\n return insertions;\n\n\n }\n\nprivate:\n int LISlen(const vector<int> seq){\n vector<int> lis;\n\n for(int i : seq){\n auto it = lower_bound(lis.begin(), lis.end(), i);\n\n if(it == lis.end()){\n lis.push_back(i);\n }\n else{\n *it = i;\n }\n }\n\n return lis.size();\n }\n};",
"memory": "14500"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int dp[505][5005]; \n\n int solve(int l,int r,string &s)\n {\n if(l==r) return 1;\n if(l>r) return 0;\n if(dp[l][r]!=-1) return dp[l][r];\n\n if(s[l]==s[r]) return 2 + solve(l+1,r-1,s);\n\n int pushl = solve(l+1,r,s);\n int pushj = solve(l,r-1,s);\n\n return dp[l][r]=max(pushl,pushj);\n }\n\n int minInsertions(string s) {\n memset(dp,-1,sizeof(dp));\n\n int n = s.size();\n int samepos = solve(0,n-1,s);\n return n-samepos;\n }\n};",
"memory": "18700"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int dp[505][5005]; \n\n int solve(int l,int r,string &s)\n {\n if(l==r) return 1;\n if(l>r) return 0;\n if(dp[l][r]!=-1) return dp[l][r];\n\n if(s[l]==s[r]) return 2 + solve(l+1,r-1,s);\n\n int pushl = solve(l+1,r,s);\n int pushj = solve(l,r-1,s);\n\n return dp[l][r]=max(pushl,pushj);\n }\n\n int minInsertions(string s) {\n memset(dp,-1,sizeof(dp));\n\n int n = s.size();\n int samepos = solve(0,n-1,s);\n return n-samepos;\n }\n};",
"memory": "18900"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int dp[505][5005]; \n\n int solve(int l,int r,string &s)\n {\n if(l==r) return 1;\n if(l>r) return 0;\n if(dp[l][r]!=-1) return dp[l][r];\n\n if(s[l]==s[r]) return 2 + solve(l+1,r-1,s);\n\n int pushl = solve(l+1,r,s);\n int pushj = solve(l,r-1,s);\n\n return dp[l][r]=max(pushl,pushj);\n }\n\n int minInsertions(string s) {\n memset(dp,-1,sizeof(dp));\n\n int n = s.size();\n int samepos = solve(0,n-1,s);\n return n-samepos;\n }\n};",
"memory": "18900"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int dp[505][5005]; \n\n int solve(int l,int r,string &s)\n {\n if(l==r) return 1;\n if(l>r) return 0;\n if(dp[l][r]!=-1) return dp[l][r];\n\n if(s[l]==s[r]) return 2 + solve(l+1,r-1,s);\n\n int pushl = solve(l+1,r,s);\n int pushj = solve(l,r-1,s);\n\n return dp[l][r]=max(pushl,pushj);\n\n }\n\n int minInsertions(string s) {\n memset(dp,-1,sizeof(dp));\n\n int n = s.size();\n int samepos = solve(0,n-1,s);\n return n-samepos;\n }\n};",
"memory": "19000"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int dp[505][5005]; \n\n int solve(int l,int r,string &s)\n {\n if(l==r) return 1;\n if(l>r) return 0;\n if(dp[l][r]!=-1) return dp[l][r];\n\n if(s[l]==s[r]) return 2 + solve(l+1,r-1,s);\n\n int pushl = solve(l+1,r,s);\n int pushj = solve(l,r-1,s);\n\n return dp[l][r]=max(pushl,pushj);\n }\n\n int minInsertions(string s) {\n memset(dp,-1,sizeof(dp));\n\n int n = s.size();\n int samepos = solve(0,n-1,s);\n return n-samepos;\n }\n};",
"memory": "19000"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minInsertions(string s) {\n string t = s;\n reverse(t.begin(),t.end());\n if(s==t) return 0;\n\n int len = s.length();\n vector<vector<int>> dp(len + 1, vector<int>(len+1,0));\n\n for(int i = 1; i <= len; i++){\n for(int j = 1; j <= len; j++){\n if(s[i-1] == t[j-1]){\n dp[i][j] = 1 + dp[i-1][j-1];\n }\n else{\n dp[i][j] = max(dp[i-1][j], dp[i][j-1]);\n }\n }\n }\n // cout << dp[len][len];\n return (len - dp[len][len]); \n }\n};",
"memory": "24800"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int ans(string &s,string &st,int i ,int j, vector<vector<int>>& dp)\n {\n if(i<0 || j<0)\n {\n return 0;\n }\n if(dp[i][j]!=-1)\n {\n return dp[i][j];\n }\n int nottake=max(ans(s,st,i-1,j,dp),ans(s,st,i,j-1,dp));\n int take=0;\n if(s[i]==st[j])\n {\n take=1+ans(s,st,i-1,j-1,dp);\n }\n return dp[i][j]=max(take,nottake);\n }\n int minInsertions(string s) {\n string st=\"\";\n int n=s.length();\n for(int i=n-1;i>=0;i--)\n {\n st+=s[i];\n }\n if(st==s)\n {\n return 0;\n }\n vector<vector<int>>dp(n,vector<int>(n,-1));\n return n-ans(s,st,n-1,n-1,dp);\n }\n};",
"memory": "24900"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minInsertions(string a) {\n string b = a;\n reverse(b.begin(),b.end());\n \n if(a==b)\n return 0;\n \n int n= a.size();\n vector<vector<int>> dp(n+1,vector<int> (n+1,-1));\n \n for(int i=0;i<=n;i++) dp[i][0] = 0;\n for(int j=0;j<=n;j++) dp[0][j] = 0;\n \n for(int i=1;i<=n;i++) {\n for(int j=1;j<=n;j++) {\n if(a[i-1] == b[j-1]) {\n dp[i][j] = 1 + dp[i-1][j-1];\n }\n else dp[i][j] = max(dp[i-1][j], dp[i][j-1]);\n }\n }\n int len = dp[n][n];\n \n return n-len;\n \n }\n};",
"memory": "25000"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minInsertions(string s) {\n // If the string is already a palindrome, return 0\n if (is_palindrome(s)) {\n return 0; \n }\n\n int n = s.size();\n // Initialize the dp table with -1 to indicate uncomputed subproblems\n vector<vector<int>> dp(n, vector<int>(n, -1)); \n\n // Start the helper function\n return minInsertionsHelper(s, 0, n - 1, dp); \n }\n\n // Helper function to calculate the minimum insertions with memoization\n int minInsertionsHelper(string &s, int left, int right, vector<vector<int>>& dp) {\n // Base case: if left >= right, no insertions are needed\n if (left >= right) {\n return 0; \n }\n\n // If the result is already computed, return it\n if (dp[left][right] != -1) {\n return dp[left][right]; \n }\n\n // If the characters match, move both pointers inward\n if (s[left] == s[right]) {\n dp[left][right] = minInsertionsHelper(s, left + 1, right - 1, dp);\n } else {\n // If they don't match, try inserting at left or right and take the minimum\n dp[left][right] = min(minInsertionsHelper(s, left + 1, right, dp), \n minInsertionsHelper(s, left, right - 1, dp)) + 1;\n }\n\n return dp[left][right]; \n }\n\n // Function to check if the string is a palindrome\n bool is_palindrome(const string &s) {\n int left = 0, right = s.size() - 1;\n while (left < right) {\n if (s[left] != s[right]) {\n return false;\n }\n left++;\n right--;\n }\n return true;\n }\n};\n",
"memory": "25100"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "bool checkPalindrome(string s){\n int size = s.size()-1;\n for(int i = 0 ;i<=size;i++){\n if(i >= (size-i)) return true; \n if(s[i]!=s[size-i]) return false;\n else continue;\n }\n return true;\n}\nint getMinPalin(string& s,int F,int L,vector<vector<int>>& dp){\n if(F>=L) return 0;\n //cout<<F<<\" \"<<L<<endl;\n int case1 = 0;\n int case2 = 0;\n if(dp[F][L]!=-1) return dp[F][L];\n if(s[F]==s[L]) case1 = getMinPalin(s,F+1,L-1,dp);\n // string k = s.substr(F,L-F+1);\n // if(checkPalindrome(k)) return 0; \n if(s[F]!=s[L]) case1 = 1+min(getMinPalin(s,F+1,L,dp),getMinPalin(s,F,L-1,dp));\n \n return dp[F][L] = case1;\n //return case1;\n\n}\nclass Solution {\npublic:\n int minInsertions(string s) {\n if(checkPalindrome(s)) return 0;\n int size = s.size();\n vector<vector<int>> dp(size+1,vector<int> (size+1,-1));\nreturn getMinPalin(s,0,size-1,dp);\n\n }\n};",
"memory": "25200"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "bool checkPalindrome(string s){\n int size = s.size()-1;\n for(int i = 0 ;i<=size;i++){\n if(i >= (size-i)) return true; \n if(s[i]!=s[size-i]) return false;\n else continue;\n }\n return true;\n}\nint getMinPalin(string& s,int F,int L,vector<vector<int>>& dp){\n if(F>=L) return 0;\n //cout<<F<<\" \"<<L<<endl;\n int case1 = 0;\n int case2 = 0;\n if(dp[F][L]!=-1) return dp[F][L];\n if(s[F]==s[L]) case1 = getMinPalin(s,F+1,L-1,dp);\n // string k = s.substr(F,L-F+1);\n // if(checkPalindrome(k)) return 0; \n if(s[F]!=s[L]) case1 = 1+min(getMinPalin(s,F+1,L,dp),getMinPalin(s,F,L-1,dp));\n \n return dp[F][L] = case1;\n //return case1;\n\n}\nclass Solution {\npublic:\n int minInsertions(string s) {\n if(checkPalindrome(s)) return 0;\n int size = s.size();\n vector<vector<int>> dp(size+1,vector<int> (size+1,-1));\nreturn getMinPalin(s,0,size-1,dp);\n\n }\n};",
"memory": "25200"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool isPalindrome(string s){\n string t = s;\n reverse(s.begin(), s.end());\n return s == t;\n }\n\n int findLCS(string s1, string s2){\n int n1 = s1.size(); int n2 = s2.size();\n\n vector<vector<int> > dp(n1+1, vector<int> (n2+1, 0));\n\n for(int i=0; i<=n1; i++) dp[i][0] = 0;\n for(int i=0; i<=n2; i++) dp[0][i] = 0;\n\n for(int i=1; i<=n1; i++){\n for(int j=1; j<=n2; j++){\n int res = 0;\n if(s1[i-1] == s2[j-1])\n res = 1 + dp[i-1][j-1];\n else\n res = max(dp[i-1][j], dp[i][j-1]);\n dp[i][j] = res;\n }\n }\n\n return dp[n1][n2];\n }\n\n int minInsertions(string s) {\n if(isPalindrome(s)) return 0;\n string t = s;\n reverse(t.begin(), t.end());\n // what if we do -> string_length - lcs\n int lcs = findLCS(s, t);\n return s.size() - lcs;\n }\n};",
"memory": "25300"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int solve(string& s, int i, int j, vector<int>& dp) {\n if (i >= j) \n return 0;\n if (dp[i * s.size() + j] != -1) \n return dp[i * s.size() + j];\n if (s[i] == s[j]) \n return dp[i * s.size() + j] = solve(s, i + 1, j - 1, dp);\n return dp[i * s.size() + j] = 1 + min(solve(s, i + 1, j, dp), solve(s, i, j - 1, dp));\n }\n\n int minInsertions(string s) {\n int n = s.size();\n vector<int> dp(n * n, -1);\n return solve(s, 0, n - 1, dp); \n }\n};\n\nauto init = []() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 0;\n}();\n",
"memory": "26200"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool palindrome(int i,int j,string s){\n if(i>=j) return 1;\n if(s[i]==s[j]) return palindrome(i+1,j-1,s);\n else return 0;\n }\n int f(int i,int j,string s,string t){\n if(i==0 || j==0) return 0;\n if(s[i-1]==t[j-1]) return 1+f(i-1,j-1,s,t);\n else return max(f(i-1,j,s,t),f(i,j-1,s,t));\n }\n int minInsertions(string s) {\n int n=s.size();\n if(palindrome(0,n-1,s)) return 0;\n string t=s;\n reverse(t.begin(),t.end());\n vector<vector<int>>dp(n+1,vector<int>(n+1,0));\n for(int i=1;i<=n;i++){\n for(int j=1;j<=n;j++){\n if(s[i-1]==t[j-1]) dp[i][j]= 1+dp[i-1][j-1];\n else dp[i][j]= max(dp[i-1][j],dp[i][j-1]);\n }\n }\n int k=dp[n][n];\n return n-k;\n }\n};",
"memory": "26400"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minInsertions(string s) {\n const int n = s.size();\n std::vector<int> dp(n);\n for (auto i = n - 2; i >= 0; --i) {\n std::vector<int> new_dp(n);\n for (auto j = i + 1; j < n; ++j) {\n if (s[j] == s[i]) {\n new_dp[j] = dp[j - 1];\n } else {\n new_dp[j] = std::min(new_dp[j - 1], dp[j]) + 1;\n }\n }\n dp = new_dp;\n }\n return dp.back(); \n }\n};",
"memory": "29100"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\nint longestCommonSubsequence(string text1, string text2) {\n vector<vector<int>> dp(text1.size(), vector<int>(text2.size(),-1));\n // return solve(text1,text2,dp,0,0);\n int i,j;\n for(i=0;i<text1.size();i++)\n {\n for(j=0;j<text2.size();j++)\n {\n if(text1[i]==text2[j])\n {\n if(i==0 || j==0)\n dp[i][j]=1;\n else\n dp[i][j]=dp[i-1][j-1]+1;\n }\n else\n {\n if(i!=0 && j!=0)\n dp[i][j]=max(dp[i-1][j],dp[i][j-1]);\n else if(j!=0)\n dp[i][j]=dp[i][j-1];\n else if(i!=0)\n dp[i][j]=dp[i-1][j];\n else\n dp[i][j]=0;\n }\n }\n }\n // for(auto x:dp)\n // {\n // cout<<endl;\n // for(auto y:x)\n // cout<<y<<\" \";\n // }\n return dp[text1.size()-1][text2.size()-1];\n }\n int minInsertions(string s) {\n string rev=\"\";\n int i;\n for(i=0;i<s.size();i++)\n {\n rev = s[i] +rev;\n }\n if(rev==s)\n return 0;\n cout<<s.substr(0,(s.size())/2)<<\" \"<<s.substr((s.size())/2);\n int l = longestCommonSubsequence(s, rev);\n // if(s.size()%2==0)\n // return s.size() - 2*l;\n // else\n return s.size() - l;\n }\n};",
"memory": "29200"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\nint longestCommonSubsequence(string text1, string text2) {\n vector<vector<int>> dp(text1.size(), vector<int>(text2.size(),-1));\n // return solve(text1,text2,dp,0,0);\n int i,j;\n for(i=0;i<text1.size();i++)\n {\n for(j=0;j<text2.size();j++)\n {\n if(text1[i]==text2[j])\n {\n if(i==0 || j==0)\n dp[i][j]=1;\n else\n dp[i][j]=dp[i-1][j-1]+1;\n }\n else\n {\n if(i!=0 && j!=0)\n dp[i][j]=max(dp[i-1][j],dp[i][j-1]);\n else if(j!=0)\n dp[i][j]=dp[i][j-1];\n else if(i!=0)\n dp[i][j]=dp[i-1][j];\n else\n dp[i][j]=0;\n }\n }\n }\n // for(auto x:dp)\n // {\n // cout<<endl;\n // for(auto y:x)\n // cout<<y<<\" \";\n // }\n return dp[text1.size()-1][text2.size()-1];\n }\n int minInsertions(string s) {\n string rev=\"\";\n int i;\n for(i=0;i<s.size();i++)\n {\n rev = s[i] +rev;\n }\n if(rev==s)\n return 0;\n // cout<<s.substr(0,(s.size())/2)<<\" \"<<s.substr((s.size())/2);\n int l = longestCommonSubsequence(s, rev);\n // if(s.size()%2==0)\n // return s.size() - 2*l;\n // else\n return s.size() - l;\n }\n};",
"memory": "29200"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minInsertions(string s) {\n int n = s.size();\n string t = s;\n reverse(t.begin(),t.end());\n vector<int>prev(n+1,0);\n for(int i=1;i<=n;i++){\n vector<int>curr(n+1,0);\n for(int j=1;j<=n;j++){\n if(s[i-1]==t[j-1]){\n curr[j] = 1+prev[j-1];\n }else{\n curr[j] = max(curr[j-1],prev[j]);\n }\n }\n prev = curr;\n }\n return n-prev[n];\n }\n};",
"memory": "29300"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minInsertions(string s) {\n string t=s;\n reverse(s.begin(),s.end());\n int n=s.length();\n\tint m=t.length();\n\t//Write your code here\n\tint **dp=new int*[n+1];\n\tfor(int i=0;i<=n;i++)\n\t{\n\t\tdp[i]=new int[m+1];\n\n\t\tfor(int j=0;j<=m;j++)\n\t\t{\n\t\t\tdp[i][j]=0;\n\t\t}\n\t}\n\t\n\n\tfor(int i=1;i<=n;i++)\n\t{\n\t\tfor(int j=1;j<=m;j++)\n\t\t{\n\t\t\tif(s[i-1]==t[j-1]){\n\t\t\t\tdp[i][j]=dp[i-1][j-1]+1;\n\t\t\t}\n\n\t\t\telse{ \n\t\t\tdp[i][j]=max(dp[i-1][j],dp[i][j-1]);}\n\t\t}\n\t}\n\n\treturn s.length()-dp[n][m];\n }\n};",
"memory": "29400"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minInsertions(string t1) {\n string t2=t1;\n reverse(t2.begin(),t2.end());\n int n=t1.size();\n int m=t2.size();\n vector<int>prev(m+1, 0);\n for(int i1=1;i1<=n;i1++){\n vector<int>curr(m+1, 0);\n for(int i2=1;i2<=m;i2++){\n if(t1[i1-1]==t2[i2-1]){\n curr[i2]=1+prev[i2-1];\n }\n else{\n curr[i2]=max(prev[i2],curr[i2-1]);\n \n }\n }\n prev=curr;\n }\n return n-prev[m];\n }\n};",
"memory": "29500"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minInsertions(string s) {\n int n = s.size();\n vector<int> pre(n + 1, 0);\n for(int i = 1; i < n + 1; i++) {\n vector<int> cur(n + 1, 0);\n for(int j = 1; j < n + 1; j++) {\n if(s[i - 1] == s[n - j])\n cur[j] = pre[j - 1] + 1;\n else\n cur[j] = max(pre[j], cur[j - 1]);\n }\n pre = cur;\n }\n return n - pre[n];\n }\n};",
"memory": "29500"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\nprivate:\nint tab(string text1, string text2,int n)\n {\n vector<int> dp(n+1,0);\n for(int i=0;i<=n;i++)\n {\n vector<int> curr(n+1,0);\n for(int j=0;j<=n;j++)\n {\n if(i==0 || j==0)curr[j]=0;\n else{\n if(text1[i-1]==text2[j-1])\n {\n curr[j]=1+dp[j-1];\n }\n else\n {\n curr[j]=max(curr[j-1],dp[j]);\n }\n }\n }\n dp=curr;\n }\n return dp[n];//longest palindromice subsequence\n }\npublic:\n int minInsertions(string s) {\n int n=s.length();\n string t=s;\n reverse(s.begin(),s.end());\n int lps=tab(s,t,n);\n return n-lps;\n }\n};",
"memory": "29600"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\n int helper(int left, int right, const string &s, vector<vector<int>> &dp) {\n if (left >= right) return 0; // No insertions needed when indices cross or single character\n\n if (dp[left][right] != -1) return dp[left][right];\n\n if (s[left] == s[right]) {\n // No insertion needed if characters match\n return dp[left][right] = helper(left + 1, right - 1, s, dp);\n }\n\n // Insert one character either on the left or the right\n return dp[left][right] = min(1 + helper(left + 1, right, s, dp), \n 1 + helper(left, right - 1, s, dp));\n }\n\npublic:\n int minInsertions(string s) {\n int n = s.size();\n vector<vector<int>> dp(n, vector<int>(n, -1)); // Memoization table\n return helper(0, n - 1, s, dp); // Start from the full range of the string\n }\n};\n",
"memory": "29700"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>>memory;\nint dp(int start,int end,string &s){\n\tif(start>=end)return 0;\n\tif(memory[start][end]!=-1)return memory[start][end];\n\tif(s[start]==s[end])return memory[start][end]=dp(start+1,end-1,s);\n\tint insert=1+dp(start+1,end,s);\n\tint del=1+dp(start,end-1,s);\n\treturn memory[start][end]=min(insert,del);\n}\n int minInsertions(string s) {\n \tmemory.resize(s.size(),vector<int>(s.size(),-1));\n \treturn dp(0,s.size()-1,s);\n }\n};",
"memory": "29700"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "#pragma GCC optimize(\"O3, unroll-loops\")\n#pragma GCC target(\"avx2, bmi, bmi2, lzcnt, popcnt\")\n\nstatic auto init = []() { \n cin.tie(nullptr) -> ios_base::sync_with_stdio(false); \n return nullptr; \n} ();\n\nclass Solution {\n public:\n int minInsertions(string s) {\n constexpr auto longestPalSubseq = [&](const string& s) -> int {\n const int n = s.length();\n // dp[i][j] := the length of LPS(s[i..j])\n vector<vector<int>> dp(n, vector<int>(n));\n for (int i = 0; i < n; ++i) dp[i][i] = 1;\n for (int d = 1; d < n; ++d)\n for (int i = 0; i + d < n; ++i) {\n const int j = i + d;\n if (s[i] == s[j]) dp[i][j] = 2 + dp[i + 1][j - 1];\n else dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]);\n }\n return dp[0][n - 1];\n };\n return s.length() - longestPalSubseq(s);\n } \n};",
"memory": "29800"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minInsertions(string s) {\n \n int n = s.size();\n vector<vector<int>> dp(n+1,vector<int>(n+1,0));\n\n for(int i=1 ; i<=n ; i++)\n {\n for(int j=1 ; j<=n ; j++)\n {\n if(s[i-1] == s[(n-1)-(j-1)]){\n dp[i][j] = 1+dp[i-1][j-1];\n }\n else{\n dp[i][j] = max(dp[i-1][j], dp[i][j-1]);\n }\n }\n }\n\n return n-dp[n][n]; \n }\n};",
"memory": "30100"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minInsertions(string str1) {\n string str2;\n int n=str1.size();\n for(int i=0;i<n;i++){\n str2.push_back(str1[n-i-1]);\n }\n vector<vector<int>>dp(n+1,vector<int>(n+1,0));\n for(int i=1;i<=n;i++){\n for(int j=1;j<=n;j++){\n if(str1[i-1]==str2[j-1]){\n\t\t dp[i][j]=1+dp[i-1][j-1];\n\t\t }\n\t\t else\n\t\t dp[i][j]=max(dp[i][j-1],dp[i-1][j]);\n }\n }\n return (n-dp[n][n]);\n }\n};",
"memory": "30100"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\n#pragma GCC optimize(\"Ofast\", \"inline\", \"unroll-loops\")\n#include <limits>\n// \nauto init = []() {\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n std::ofstream out(\"user.out\");\n int num;\n do {\n std::cin.clear(); // Clear any error flags\n std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\\n'); \n std::cin >> num;\n if (std::cin.fail()) continue;\n out << (num != -1 ? \"true\\n\" : \"false\\n\");\n } while (!std::cin.eof());\n out.flush();\n exit(0);\n return 0;\n}();\n\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n return false;\n }\n};",
"memory": "7300"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\n#pragma GCC optimize(\"Ofast\", \"inline\", \"unroll-loops\")\n#include <limits>\n\nauto init = []() {\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n std::ofstream out(\"user.out\");\n int num;\n do {\n std::cin.clear(); // Clear any error flags\n std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\\n'); \n std::cin >> num;\n if (std::cin.fail()) continue;\n out << (num != -1 ? \"true\\n\" : \"false\\n\");\n } while (!std::cin.eof());\n out.flush();\n exit(0);\n return 0;\n}();\n\nclass Solution {\npublic:\n bool hasCycle(ListNode* head) {\n\n // logic is same as Cap. America shouting behind you to Falcon, if there\n // are fast and slow pointers in a cycle, or circular track, the faster\n // one while running, will come across the slower one (Falcon) at the\n // same position (when overtaking) at somepoint\n\n ListNode* slow = head;\n ListNode* fast = head;\n while (slow != nullptr && fast != nullptr && fast->next != nullptr) {\n slow = slow->next;\n fast = fast->next->next;\n\n if (slow == fast)\n return true;\n }\n return false;\n }\n};",
"memory": "7400"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n if(head==NULL){\n return false;\n }\n if(head->next==NULL){\n return false;\n }\n ListNode* slow=head;\n ListNode* fast=head;\n while(fast->next &&fast->next->next){\n slow=slow->next;\n fast=fast->next->next;\n if(fast==slow){\n return true;\n }\n \n }\n return false;\n }\n};",
"memory": "10800"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n if(head == NULL)\n return false;\n ListNode *a = head, *b = head;\n while(a-> next != NULL && b->next != NULL && b->next->next != NULL) {\n a = a->next;\n b = b->next->next;\n if(a == b)\n return true;\n }\n return false;\n }\n};",
"memory": "10800"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 1 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n if(head==NULL) return false;\n if(head->next==nullptr) return false;\n ListNode *slow=head;\n ListNode *fast=head;\n while(fast!=NULL && fast->next!=NULL)\n {\n slow=slow->next;\n fast=fast->next->next;\n if(slow==fast)\n {\n return true;\n }\n }\n return false;\n \n }\n};",
"memory": "10900"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 1 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n if(head==NULL || head->next==nullptr) return false;\n ListNode *slow=head;\n ListNode *fast=head;\n while(fast!=NULL && fast->next!=NULL)\n {\n slow=slow->next;\n fast=fast->next->next;\n if(slow==fast)\n {\n return true;\n }\n }\n return false;\n \n }\n};",
"memory": "10900"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n ListNode *slow=head,*fast=head;\n if(head==NULL || head->next==NULL)return false;\n while(slow && fast && fast->next){\n slow=slow->next;\n fast=fast->next->next;\n if(slow==fast)return true;\n //head=head->next;\n }\n return false;\n }\n};",
"memory": "11000"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "class Solution {\npublic:\n bool hasCycle(ListNode *head) {\n // Check if the list is empty or has only one node (no cycle possible)\n if (head == NULL || head->next == NULL) {\n return false;\n }\n \n // Initialize two pointers: slow moves 1 step, fast moves 2 steps\n ListNode *slow = head;\n ListNode *fast = head;\n \n // Traverse the list\n while (fast != NULL && fast->next != NULL) {\n // Move slow pointer by 1 and fast pointer by 2\n slow = slow->next;\n fast = fast->next->next;\n \n // If slow and fast meet, a cycle exists\n if (slow == fast) {\n return true;\n }\n }\n \n // If no cycle is found, return false\n return false;\n }\n};\n",
"memory": "11000"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n if(head==nullptr)\n return false;\n ListNode* start=head;\n int hash[100000]={0};\n while(head->next!=NULL)\n { hash[head-start]++;\n if(hash[head-start]>1)\n {\n return true;\n }\n head=head->next;\n \n }\n return false;\n }\n};",
"memory": "11100"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n \n try {\n if (head == nullptr || head->next == nullptr) {\n throw std::runtime_error(\"No cycle detected\");\n }\n\n ListNode* fast = head;\n ListNode* slow = head;\n\n while (true) {\n if (fast == nullptr || fast->next == nullptr) {\n throw std::runtime_error(\"No cycle detected\");\n }\n\n slow = slow->next;\n fast = fast->next->next;\n\n if (slow == fast) {\n return true; // Cycle detected\n }\n }\n } \n catch (const std::runtime_error& e) {\n return false; // No cycle detected\n } \n\n return false;\n }\n};",
"memory": "11200"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n if(head==nullptr)\n return false;\n ListNode* start=head;\n int hash[100000]={0};\n while(head->next!=NULL)\n { hash[head-start]++;\n if(hash[head-start]>1)\n {\n return true;\n }\n head=head->next;\n \n }\n return false;\n }\n};",
"memory": "11300"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n ListNode dummy;\n return HasCycleImpl( &dummy, head );\n }\n\n bool HasCycleImpl(ListNode* dummy, ListNode* node )\n {\n if( node == nullptr )\n return false;\n if( node->next == nullptr )\n return false;\n if( node->next == dummy )\n return true;\n\n auto next= node->next;\n node->next= dummy;\n bool has_cycle= HasCycleImpl( dummy, next );\n node->next= next;\n return has_cycle;\n }\n};",
"memory": "11400"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n vector <ListNode* > arr;\n while(head!=NULL){\n arr.push_back(head);\n head=head->next;\n for (int i=0; i<arr.size(); i++){\n if (head==arr[i]){\n return true;\n }\n }\n }\n return false;\n }\n};",
"memory": "11500"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n ListNode* temp = head;\n vector<ListNode*>ans;\n while(temp != NULL){\n if(find(ans.begin(), ans.end(), temp) != ans.end()){\n return true;\n }\n ans.push_back(temp);\n temp = temp->next;\n }\n return false;\n }\n};",
"memory": "11600"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode* head) {\n ListNode* ptr;\n ptr = head;\n vector<ListNode*> temp;\n while (ptr) {\n if (ptr->next == nullptr)\n return false;\n if (find(temp.begin(), temp.end(), ptr->next) != temp.end())\n return true;\n temp.push_back(ptr->next);\n ptr = ptr->next;\n }\n return 0;\n }\n};",
"memory": "11600"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n ListNode* curr=head;\n vector<ListNode*>arr;\n while(curr!=NULL){\n for(int i=0;i<arr.size();i++){\n if(arr[i]==curr){\n return true;\n }\n }\n arr.push_back(curr);\n curr=curr->next;\n }\n return false; \n }\n};",
"memory": "11700"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n ListNode *p=head;\n if(p==NULL||p->next==NULL)\n return false;\n std::vector<ListNode *> Nodeaddress;\n while(p!=NULL)\n {\n if(std::find(Nodeaddress.begin(), Nodeaddress.end(), p)!=Nodeaddress.end())\n return true;\n Nodeaddress.push_back(p);\n p=p->next;\n }\n return false;\n }\n};",
"memory": "11800"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n\n vector <ListNode*> A;\n while (head != nullptr){\n for (auto x: A){\n if (x == head){\n return true;\n }\n }\n A.push_back(head);\n head = head->next;\n \n }\n return false;\n }\n};",
"memory": "11900"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n struct ListNode1 {\n ListNode* val;\n ListNode1 *next;\n ListNode1(ListNode* x) : val(x), next(NULL) {}\n };\n\n bool hasCycle(ListNode *head) {\n if (!head) return false;\n ListNode1* temp = new ListNode1(head);\n head = head->next;\n while (head != NULL) {\n ListNode1* temp2 = temp;\n while (temp2 != NULL) {\n if (temp2->val == head) {\n return true;\n }\n temp2 = temp2->next;\n }\n ListNode1* temp1 = new ListNode1(head);\n temp1->next = temp;\n temp = temp1;\n head = head->next;\n }\n return false;\n }\n};\n",
"memory": "12000"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n struct ListNode1 {\n ListNode* val;\n ListNode1 *next;\n ListNode1(ListNode* x) : val(x), next(NULL) {}\n };\n\n bool hasCycle(ListNode *head) {\n if (!head) return false;\n ListNode1* temp = new ListNode1(head);\n head = head->next;\n while (head != NULL) {\n ListNode1* temp2 = temp;\n while (temp2 != NULL) {\n if (temp2->val == head) {\n return true;\n }\n temp2 = temp2->next;\n }\n ListNode1* temp1 = new ListNode1(head);\n temp1->next = temp;\n temp = temp1;\n head = head->next;\n }\n return false;\n }\n};\n",
"memory": "12100"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n struct ListNode1 {\n ListNode* val;\n ListNode1 *next;\n ListNode1(ListNode* x) : val(x), next(NULL) {}\n };\n\n bool hasCycle(ListNode *head) {\n if (!head) return false;\n ListNode1* temp = new ListNode1(head);\n head = head->next;\n while (head != NULL) {\n ListNode1* temp2 = temp;\n while (temp2 != NULL) {\n if (temp2->val == head) {\n return true;\n }\n temp2 = temp2->next;\n }\n ListNode1* temp1 = new ListNode1(head);\n temp1->next = temp;\n temp = temp1;\n head = head->next;\n }\n return false;\n }\n};\n",
"memory": "12200"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head)\n {\n vector <int> prevs;\n int i = 0, n = 0;\n //int *foo;\n \n for (auto p = head; p; p = p->next)\n {\n //foo = std::find(std::begin(prevs), std::end(prevs), p->val);\n // When the element is not found, std::find returns the end of the range\n if (std::find(std::begin(prevs), std::end(prevs), p->val) != std::end(prevs)) {\n //cerr << \"Found at position \" << std::distance(prevs, foo) << endl;\n n++;\n } else {\n n = 0;\n cerr << \"Not found\" << endl;\n }\n prevs.insert(prevs.begin(), p->val);\n if (n >= 6)\n return true;\n }\n return false;\n }\n};",
"memory": "12300"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head)\n {\n vector <int> prevs;\n int i = 0, n = 0;\n //int *foo;\n \n for (auto p = head; p; p = p->next)\n {\n //foo = std::find(std::begin(prevs), std::end(prevs), p->val);\n // When the element is not found, std::find returns the end of the range\n if (std::find(std::begin(prevs), std::end(prevs), p->val) != std::end(prevs)) {\n //cerr << \"Found at position \" << std::distance(prevs, foo) << endl;\n n++;\n } else {\n n = 0;\n cerr << \"Not found\" << endl;\n }\n prevs.insert(prevs.begin(), p->val);\n if (n >= 10)\n return true;\n }\n return false;\n }\n};",
"memory": "12400"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head)\n {\n vector <int> prevs;\n int i = 0, n = 0;\n \n for (auto p = head; p; p = p->next)\n {\n if (std::find(std::begin(prevs), std::end(prevs), p->val) != std::end(prevs))\n n++;\n else\n n = 0;\n prevs.insert(prevs.begin(), p->val);\n if (n >= 10)\n return true;\n }\n return false;\n }\n};",
"memory": "12600"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution\n{\npublic:\n bool checkDuplicate( vector<ListNode *> &arr, ListNode *x)\n {\n for (int i = 0; i < arr.size(); i++)\n {\n if (x == arr[i])\n {\n return true;\n }\n }\n arr.push_back(x);\n return false;\n }\n\n bool hasCycle(ListNode *head)\n {\n ListNode *current = head;\n vector<ListNode *> arr;\n\n while (current != nullptr)\n {\n if (checkDuplicate(arr, current))\n {\n return true;\n }\n current = current->next;\n }\n return false;\n }\n};\n",
"memory": "12700"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n std::vector<ListNode*> arr;\n bool check(ListNode *ptr)\n {\n for (int i=0;i<arr.size();i++){\n if(ptr == arr[i])\n {\n return false;\n }\n }\n arr.push_back(ptr);\n return true;\n }\n bool hasCycle(ListNode *head) {\n while(head!=nullptr){\n if(check(head))\n {\n head=head->next;\n }\n else return true;\n }\n return false;\n }\n};",
"memory": "12800"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n if(head == NULL) return false;\n set<ListNode*> stt;\n ListNode* temp = head;\n while(temp -> next != NULL){\n if(stt.find(temp) != stt.end()) return true;\n stt.insert(temp);\n temp = temp -> next; \n }\n return false;\n }\n};",
"memory": "13900"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n std::set<ListNode*> visited;\n ListNode* curr = head;\n while(curr != nullptr) {\n if (visited.count(curr) == 0) {\n visited.insert(curr);\n } else {\n return true;\n }\n curr = curr->next;\n }\n return false;\n }\n};",
"memory": "13900"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n set<ListNode *> l;\n ListNode *th=head;\n while(th)\n {\n if(l.find(th)!=l.end())\n return true;\n l.insert(th);\n th=th->next;\n }\n return false;\n }\n};",
"memory": "14000"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n set<ListNode *> visited;\n while (head != nullptr) {\n if (visited.find(head) != visited.end()) return true;\n visited.insert(head);\n head = head->next;\n }\n return false;\n }\n};",
"memory": "14000"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n set<ListNode*> nodesAddr;\n ListNode* curr = head;\n\n while(curr) {\n if (nodesAddr.find(curr) != nodesAddr.end()) {\n return true;\n }\n\n nodesAddr.insert(curr);\n curr = curr->next;\n }\n\n return false;\n }\n};",
"memory": "14100"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n if(head==NULL)\n {\n return false;\n }\n set<ListNode*> st;\n st.insert(head);\n head=head->next;\n while(head!=NULL)\n {\n if(st.count(head))\n {\n return true;\n }\n st.insert(head);\n head=head->next;\n }\n return false;\n }\n};",
"memory": "14100"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n unordered_set<ListNode*> hash;\n while (head) { \n if (hash.find(head) != hash.end()) return true; \n hash.insert(head); \n head = head -> next; \n }\n return false;\n }\n};",
"memory": "14200"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode* head) {\n bool answer = false;\n std::unordered_set<ListNode *> visited_nodes;\n\n while (head != nullptr) {\n if (visited_nodes.find(head) != visited_nodes.end()) {\n answer = true;\n break;\n }\n visited_nodes.insert(head);\n head = head->next;\n }\n\n return answer;\n }\n};\n",
"memory": "14300"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n unordered_set<ListNode*> set;\n while (head) { \n if (set.count(head)) { \n return true;\n }\n set.insert(head);\n head = head->next;\n }\n return false;\n }\n};",
"memory": "14400"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "#include <unordered_set>\n\n/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n // 해시 테이블을 사용하여 노드의 주소를 저장\n std::unordered_set<ListNode*> visitedNodes;\n \n // 현재 노드 포인터\n ListNode* current = head;\n \n // 리스트 순회\n while (current != nullptr) {\n // 만약 현재 노드가 이미 해시 테이블에 있다면 사이클 존재\n if (visitedNodes.find(current) != visitedNodes.end()) {\n return true;\n }\n \n // 현재 노드를 해시 테이블에 저장\n visitedNodes.insert(current);\n \n // 다음 노드로 이동\n current = current->next;\n }\n \n // 노드를 모두 순회해도 사이클이 없으면 false 반환\n return false;\n }\n};\n",
"memory": "14400"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode* head) {\n unordered_set<ListNode*> hashSet;\n\n while (head != nullptr) {\n if (hashSet.find(head) != hashSet.end())\n return true;\n\n hashSet.insert(head);\n head = head -> next;\n }\n\n return false;\n }\n};",
"memory": "14500"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n unordered_set<ListNode*> nodesAddr;\n ListNode* curr = head;\n\n while(curr) {\n if (nodesAddr.count(curr)) {\n return true;\n }\n\n nodesAddr.insert(curr);\n curr = curr->next;\n }\n\n return false;\n }\n};",
"memory": "14500"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n unordered_set<ListNode*>st;\n while (head != nullptr){\n if (st.find(head) != st.end()){\n return true;\n }\n else{\n st.insert(head);\n head = head -> next;\n }\n }\n return false;\n \n }\n};",
"memory": "14600"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "#include <unordered_set>\n\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n std::unordered_set<ListNode*> visited; // Set to store visited nodes\n \n while (head != NULL) {\n if (visited.find(head) != visited.end()) { // Cycle found\n return true;\n }\n visited.insert(head); // Mark this node as visited\n head = head->next;\n }\n \n return false; // No cycle\n }\n};\n",
"memory": "14600"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n \n ListNode* temp = head;\n unordered_set<ListNode*> p;\n\n while(temp != nullptr){\n \n if(p.find(temp) != p.end()){\n return true;\n }\n \n p.insert(temp);\n temp = temp -> next;\n }\n\n return false;\n }\n};",
"memory": "14700"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n map<ListNode*,int>mpp;\n ListNode*temp=head;\n while(temp!=NULL){\n if(mpp.find(temp)!=mpp.end()){\n return true;\n }\n mpp[temp]=1;\n temp=temp->next;\n }\n return false;\n }\n};",
"memory": "14800"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n if(head==NULL)\n {\n return false;\n }\n\n map <ListNode*,bool> v;\n ListNode* temp=head;\n while(temp!=NULL)\n {\n if(v[temp]==true)\n {\n return true;\n }\n v[temp]=true;\n temp=temp->next;\n }\n\n return false;\n }\n};",
"memory": "14900"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n map<ListNode *, int> mpp;\n ListNode*temp=head;\n while(temp){\n mpp[temp->next]++;\n if(mpp[temp]==2) \n return true;\n temp=temp->next;\n \n }\n\n return false;\n }\n};",
"memory": "14900"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n map<ListNode*,bool>table;\n ListNode*temp=head;\n while(temp!=NULL){\n if(table[temp]==false){\n table[temp]=true;\n }\n else{\n return true;\n }\n temp=temp->next;\n }\n return false;\n \n }\n};",
"memory": "15000"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n map<ListNode*, int>hash;\n ListNode* temp = head;\n\n while(temp!=nullptr)\n {\n if(hash.find(temp)!=hash.end()) return true;\n\n hash[temp]=1;\n temp=temp->next;\n }\n return false;\n }\n};",
"memory": "15000"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n std::set<ListNode*> s;\n ListNode *ptr = head;\n while(ptr != nullptr) {\n if(auto it = s.find(ptr); it != s.end()) {\n return true;\n }\n s.emplace(ptr);\n ptr = ptr -> next;\n }\n return false;\n }\n};",
"memory": "15100"
} |
141 | <p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to. <strong>Note that <code>pos</code> is not passed as a parameter</strong>.</p>
<p>Return <code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="width: 141px; height: 74px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="width: 45px; height: 45px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool hasCycle(ListNode *head) {\n ListNode* temp=head;\n map<ListNode*,int>mp;\n while(temp!=NULL){\n if(mp.find(temp->next)!=mp.end()){\n return true;\n }\n mp[temp->next]=1;\n temp=temp->next;\n }\n return false;\n }\n};",
"memory": "15100"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n void reorderList(ListNode* head) {\n unordered_map<int, ListNode*> m;\n ListNode* temp = head;\n int len = 0;\n while (temp != NULL) {\n m[len] = temp;\n temp = temp->next;\n len++;\n }\n\n int left=0;\n int right=len-1;\n while(left<right){\n m[left]->next=m[right];\n left++;\n if(left==right){\n break;\n }\n m[right]->next = m[left];\n right--;\n }\n\n m[left]->next=NULL;\n\n }\n};\nint a[50001];\nint init = [] {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n ofstream out(\"user.out\");\n for (string s; getline(cin, s);) {\n if (s.length() == 2) {\n out << \"[]\\n\";\n continue;\n }\n int n = 0;\n for (int _i = 1, _n = s.length(); _i < _n; ++_i) {\n bool _neg = false;\n if (s[_i] == '-') ++_i, _neg = true;\n int v = s[_i++] & 15;\n while ((s[_i] & 15) < 10) v = v * 10 + (s[_i++] & 15);\n if (_neg) v = -v;\n a[n++] = v;\n }\n \n int i=0,j=n-1;\n out<<'[';\n while(i<j){\n out<<a[i++]<<','<<a[j--]<<\",\";\n }\n if(i==j) out<<a[i]<<',';\n out.seekp(-1,ios::end);\n out<<\"]\\n\";\n }\n out.flush();\n exit(0);\n return 0;\n}();",
"memory": "7700"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* mergeListAlternate(ListNode* head1, ListNode* head2) {\n ListNode itr(0);\n ListNode* prev = &itr;\n while (head1 != NULL && head2 != NULL) {\n prev->next = head1;\n head1 = head1->next;\n prev = prev->next;\n\n prev->next = head2;\n head2 = head2->next;\n prev = prev->next;\n }\n\n if (head1 != NULL) {\n prev->next = head1;\n } else if(head2 != NULL) {\n prev->next = head2;\n }\n return itr.next;\n }\n void reorderList(ListNode* head) {\n if(!head || !head->next) return;\n\n ListNode* slow = head;\n ListNode* fast = head;\n\n while (fast != NULL && fast->next != NULL) {\n slow = slow->next;\n fast = fast->next->next;\n }\n ListNode* second = slow->next;\n slow->next = NULL; \n ListNode* prev = NULL;\n ListNode* curr = second;\n ListNode* temp = NULL;\n\n while (curr) {\n temp = curr->next;\n curr->next = prev;\n prev = curr;\n curr = temp;\n }\n second = prev;\n head = mergeListAlternate(head, second);\n }\n};\n\n\nint a[50001];\nint init = [] {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n ofstream out(\"user.out\");\n for (string s; getline(cin, s);) {\n if (s.length() == 2) {\n out << \"[]\\n\";\n continue;\n }\n int n = 0;\n for (int _i = 1, _n = s.length(); _i < _n; ++_i) {\n bool _neg = false;\n if (s[_i] == '-') ++_i, _neg = true;\n int v = s[_i++] & 15;\n while ((s[_i] & 15) < 10) v = v * 10 + (s[_i++] & 15);\n if (_neg) v = -v;\n a[n++] = v;\n }\n \n int i=0,j=n-1;\n out<<'[';\n while(i<j){\n out<<a[i++]<<','<<a[j--]<<\",\";\n }\n if(i==j) out<<a[i]<<',';\n out.seekp(-1,ios::end);\n out<<\"]\\n\";\n }\n out.flush();\n exit(0);\n return 0;\n}();",
"memory": "7800"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n void reorderList(ListNode* head) {\n ListNode* first = head;\n while(first->next != NULL && first->next->next != NULL){\n ListNode* p = first;\n while(p->next->next != NULL){\n p = p->next;\n }\n p->next->next = first->next;\n first->next = p->next;\n p->next = NULL;\n first = first->next->next;\n }\n }\n};",
"memory": "22700"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n void reorderList(ListNode* head) {\n if (!head || !head->next)\n return;\n\n ListNode* mid = findMid(head);\n ListNode* reversed = reverse(mid);\n merge(head, reversed);\n }\n\n private:\n ListNode* findMid(ListNode* head) {\n ListNode* prev = nullptr;\n ListNode* slow = head;\n ListNode* fast = head;\n\n while (fast != nullptr && fast->next != nullptr) {\n prev = slow;\n slow = slow->next;\n fast = fast->next->next;\n }\n prev->next = nullptr;\n\n return slow;\n }\n\n ListNode* reverse(ListNode* head) {\n ListNode* prev = nullptr;\n ListNode* curr = head;\n\n while (curr != nullptr) {\n ListNode* next = curr->next;\n curr->next = prev;\n prev = curr;\n curr = next;\n }\n\n return prev;\n }\n\n void merge(ListNode* l1, ListNode* l2) {\n while (l2) {\n ListNode* next = l1->next;\n l1->next = l2;\n l1 = l2;\n l2 = next; \n }\n }\n};",
"memory": "22700"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 1 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n void reorderList(ListNode* head) {\n ListNode* slow;\n ListNode* fast;\n slow=head; \n fast=head->next;\n while(fast && fast->next){\n slow=slow->next;\n fast=fast->next->next;\n }\n ListNode* mid=slow->next;\n ListNode* prev=NULL;\n ListNode* curr=mid;\n ListNode* temp=NULL;\n slow->next=NULL;\n while(curr)\n {\n temp=curr->next;\n curr->next=prev;\n prev=curr;\n curr=temp;\n }\n \n ListNode* second;\n ListNode* first;\n ListNode* temp1;\n ListNode* temp2;\n first=head;\n second=prev;\n while(second)\n {\n temp1=first->next;\n temp2=second->next;\n first->next=second;\n second->next=temp1;\n first=temp1;\n second=temp2;\n }\n }\n \n};",
"memory": "22800"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 1 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* reverseLL(ListNode* head) {\n ListNode *prev = NULL, *curr = head, *forward;\n\n while(curr != NULL) {\n forward = curr->next;\n curr->next = prev;\n prev = curr;\n curr = forward;\n }\n\n return prev;\n } \n void reorderList(ListNode* head) {\n ListNode *slow = head, *fast = head->next;\n\n while(fast != NULL && fast->next != NULL) {\n slow = slow->next;\n fast = fast->next->next;\n }\n\n ListNode *fll = head;\n ListNode *sll_temp = slow->next;\n slow->next = NULL; //this is splitting given LL into first LL(fll) and second LL(sll)\n\n ListNode *sll = reverseLL(sll_temp); //reversing the second LL\n\n ListNode *temp1, *temp2;\n\n while(sll != NULL) {\n temp1 = fll->next;\n temp2 = sll->next;\n fll->next = sll;\n sll->next = temp1;\n fll = temp1;\n sll = temp2;\n }\n\n \n\n \n }\n};",
"memory": "22800"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 2 | {
"code": "#define opt() ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);\nclass Solution {\npublic:\n void reorderList(ListNode* head) {\n opt();\n if(head == NULL || head->next == NULL) return;\n\n ListNode * slow = head;\n ListNode * firstList = head; // TO MERGE THE FIRST HALF AND REVERSED SECOND HALF\n ListNode * fast = head;\n ListNode * prevTail = NULL; // THIS IS THE LAST NODE IN THE FIRST HALF\n\n while(fast && fast->next){\n prevTail = slow;\n slow = slow->next;\n fast = fast->next->next;\n }\n prevTail->next = NULL;\n ListNode *secondList = reverse(slow); // REVERSE THE SECOND HALF OF LL\n merge(firstList,secondList); // MERGE BOTH HALVES\n }\n ListNode *reverse(ListNode *head){ // BASIC REVERSAL OF LL\n ListNode *prev = NULL;\n ListNode *curr = head, *nextNode = NULL;\n\n while(curr != NULL){\n nextNode = curr->next;\n curr->next = prev;\n prev = curr;\n curr = nextNode;\n }\n return prev;\n }\n void merge(ListNode *l1, ListNode *l2){\n while(l2 != NULL){ // L1 || L2 != NULL ARE THE SAME THING HERE AS LIST IS SPLIT FROM MIDDLE\n ListNode *ptr1 = l1->next, *ptr2 = l2->next;\n l1->next = l2;\n\n if(ptr1 == NULL) break;\n\n l2->next = ptr1;\n l1 = ptr1;\n l2 = ptr2;\n }\n }\n};",
"memory": "22900"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 2 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n void reorderList(ListNode* head) {\n if (!head || !head->next || !head->next->next) return;\n\n ListNode* slow = head;\n ListNode* fast = head;\n \n while (fast->next && fast->next->next) {\n slow = slow->next;\n fast = fast->next->next;\n }\n ListNode* secondHalf = slow->next;\n slow->next = nullptr; \n stack<ListNode*> st;\n \n while (secondHalf) {\n st.push(secondHalf);\n secondHalf = secondHalf->next;\n }\n \n \n ListNode* firstHalf = head;\n while (!st.empty()) {\n ListNode* top = st.top();\n st.pop();\n \n ListNode* temp = firstHalf->next;\n firstHalf->next = top;\n top->next = temp;\n \n firstHalf = temp;\n }\n if (firstHalf) \n firstHalf->next = nullptr;\n \n }\n};",
"memory": "23000"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n void reorderList(ListNode *head) {\n ListNode *slow = head, *fast = head->next;\n while (fast && fast->next) {\n fast = fast->next->next;\n slow = slow->next;\n }\n\n auto *mid = slow->next;\n slow->next = nullptr;\n // printf(\"mid=%d\\n\", mid->val);\n std::stack<ListNode *> stk;\n while (mid) {\n stk.push(mid);\n mid = mid->next;\n }\n\n ListNode *p = head;\n while (!stk.empty()) {\n auto *next = p->next;\n auto *top = stk.top();\n // printf(\"link %d after %d\\n\", top->val, p->val);\n stk.pop();\n p->next = top;\n top->next = next;\n p = next;\n }\n }\n};",
"memory": "23000"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 2 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n void reorderList(ListNode* head) {\n vector<int> v;\n ListNode *temp = head;\n while(temp != NULL)\n {\n v.push_back(temp->val);\n temp = temp->next;\n }\n temp = head;\n int i = 0, j = v.size()-1;\n bool toggle = true;\n \n while (temp != nullptr) {\n if (toggle) {\n temp->val = v[i++];\n } else {\n temp->val = v[j--];\n }\n toggle = !toggle;\n temp = temp->next;\n }\n }\n};",
"memory": "23100"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 2 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n void reorderList(ListNode* head) {\n stack<ListNode*>st;\n ListNode* h=head;\n int l=0;\n while(h){\n l++;\n h=h->next;\n }\n h=head;\n l=l/2+l%2 ;\n while(h) {\n if(l==0)\n st.push(h);\n else l--;\n h=h->next;\n }\n h=head;\n while(!st.empty()){\n cout<<\"F\";\n auto x=st.top();\n auto t=h->next;\n h->next=x;\n h->next->next=t;\n st.pop();\n h=t;\n }\n if (h) {\n h->next = nullptr;\n }\n\n\n }\n};",
"memory": "23100"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 2 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n void reorderList(ListNode* head) {\n stack<ListNode*> stk;\n ListNode* slow = head;\n ListNode* fast = head;\n while (fast->next && fast->next->next) {\n stk.push(slow);\n slow = slow->next;\n fast = fast->next->next;\n }\n if (fast->next) // count of nodes is even\n slow = slow->next;\n while (slow->next) {\n ListNode* curr = slow->next;\n slow->next = slow->next->next;\n ListNode* prev = stk.top();\n stk.pop();\n curr->next = prev->next;\n prev->next = curr;\n }\n }\n};",
"memory": "23200"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 2 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n void reorderList(ListNode* head) {\n stack<ListNode*> stk;\n ListNode* slow = head;\n ListNode* fast = head;\n while (fast->next && fast->next->next) {\n stk.push(slow);\n slow = slow->next;\n fast = fast->next->next;\n }\n // count of nodes is even\n if (fast->next)\n slow = slow->next;\n while (slow->next) {\n ListNode* curr = slow->next;\n slow->next = slow->next->next;\n ListNode* prev = stk.top();\n stk.pop();\n curr->next = prev->next;\n prev->next = curr;\n }\n }\n};",
"memory": "23200"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.