Great solution. I have the same idea but was struggling on how to implement it.
However I am confused why in the end return dummyNode.next instead of head.
shouldn't dummyNode.next==head? but when I tried the latter, the code is not accepted.
Would you elaborate this part a little bit?@Google
Thanks!!!
R
RollerCoaster
@RollerCoaster
1
Reputation
6
Posts
104
Profile views
0
Followers
0
Following
Posts made by RollerCoaster

RE: Python one pass iterative solution

solution other than recursion and fibonacci
This solution takes a different approach using arrangement.
def climbStairs(n): """ :type n: int :rtype: int """ # i is how many 2step moves you take. constraints: i is int and 2*i<=n, so max i = n//2 # if take i 2step moves and j 1step moves, then j=n2*i, and total number of moves is i+j=ni factorial={0:1} # to store factorial results for faster run for i in range(1,n+1): factorial[i]=factorial[i1]*i count=0 for i in range(0,n//2+1): count+=factorial[ni]/(factorial[i]*factorial[(n2*i)]) return count

Python solution with recursion
class Solution(object): def plusOne(self, digits): """ :type digits: List[int] :rtype: List[int] """ if digits==[]: return [] if digits[1]!=9: digits[1]+=1 else: N=len(digits) if N==1: digits.insert(0,1) digits[1]=0 else: digits[1]=0 digits[:N1]=self.plusOne(digits[:N1]) return digits

Python3 solution 109ms
I'm a coding newbie... and am very surprised to see this time OJ said I beat 100% runtime among Python 3 submissions! feels I had to record it :) as I may never get this again :)
def removeElements(self, head, val): while head!=None and head.val==val: head=head.next if head==None: return else: prev=curr=head while curr!=None: curr=curr.next while curr!=None and curr.val==val: curr=curr.next prev.next=curr prev=curr return head

RE: My Python solution based on 2 sum, 200 ms beat 93.37%
Very nice to see a different idea here. Brilliant! Thank you!

python solution with one pointer
use one pointer p starts from list end and travels backwards. 32 ms run time
'''
class Solution(object):
def twoSum(self, numbers, target):
p=len(numbers)1
for i, n in enumerate(numbers):
while p>i and numbers[p]>targetn:
p=1
if numbers[p2]==targetn:
return [i+1,p+1]
'''