leetcode-1 | Solutions to all LeetCode problems : Algorithms/Ruby
kandi X-RAY | leetcode-1 Summary
kandi X-RAY | leetcode-1 Summary
Solutions to all LeetCode problems: Algorithms/Ruby, Database/MySQL, Shell/Bash.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of leetcode-1
leetcode-1 Key Features
leetcode-1 Examples and Code Snippets
Community Discussions
Trending Discussions on leetcode-1
QUESTION
I am trying to solve leetcode-148 (https://leetcode.com/problems/sort-list/) i.e. sort given LinkedList, but am getting a stackoverflow error. so far I have tried dry running but am not seeing where the issue could occur.. the base condition of the recursion seems to be right but looks like I am missing something if someone sees what I am not seeing..
...ANSWER
Answered 2022-Mar-11 at 20:16There are two issues:
When
sortList
is called with a list that has 2 nodes, then after the first loop (which makes no iterations),slow
will be equal tohead
, andfollow.next = null
will just mutate the dummy node that was prepended before thehead
node. So essentially the linked list wasn't split, and the recursive call is on the same list, leading to infinite recursion. Solve this by changing thewhile
condition so at least one iteration will be made. Also, you can do this without a third reference (follow
).In
merge
, thefirst
andsecond
references do not move forward, so the loop will be infinite.
Here is corrected code:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install leetcode-1
On a UNIX-like operating system, using your system’s package manager is easiest. However, the packaged Ruby version may not be the newest one. There is also an installer for Windows. Managers help you to switch between multiple Ruby versions on your system. Installers can be used to install a specific or multiple Ruby versions. Please refer ruby-lang.org for more information.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page