# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def mergeTwoLists(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ head=None cur=None while l1 is not None or l2 is not None: if head is None: if l1 is not None and l2 is not None and l1.val<l2.val: head=l1 l1=l1.next cur=head elif l1 is not None and l2 is not None and l1.val>l2.val: head=l2 l2=l2.next cur=head elif l1 is None and l2 is not None: head=l2 l2=l2.next cur=head else: head=l1 l1=l1.next cur=head elif l1 is not None and l2 is not None and l1.val<l2.val: cur.next=l1 l1=l1.next cur=cur.next elif l1 is not None and l2 is not None and l1.val>l2.val: cur.next=l2 l2=l2.next cur=cur.next elif l1 is None and l2 is not None: cur.next=l2 l2=l2.next cur=cur.next else: cur.next=l1 l1=l1.next cur=cur.next return head

# My backyard.

Here you can find things that interesting for me in programming at the moment

## Tuesday, April 17, 2018

### Beat 99.95% of submitted solutions for Merge Two Sorted Linked Lists

Yes, I did it again. Simple enough.

## Monday, April 9, 2018

### Beat 95.56% of python submissions

Good result. I'm happy.

# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def removeNthFromEnd(self, head, n): """ :type head: ListNode :type n: int :rtype: ListNode """ count=0 tmp=list() cur=head while cur is not None: # if count>n: # break tmp.append(cur) cur=cur.next count+=1 if count==1 and n==1: return None if n==1: tmp[len(tmp)-n-1].next=None return head if n==len(tmp): head=None head=tmp[1] return head if n>0: tmp[len(tmp)-n-1].next=tmp[len(tmp)-n+1] return head if __name__ =='__main__': s=Solution() class ListNode: def __init__(self, x): self.val = x self.next = None self.head=self def add_to_the_end(self, val): cur=self.head while cur.next is not None: cur=cur.next cur.next=ListNode(val) def printLinkedList(self): cur =self.head while cur is not None: print (cur.val) cur=cur.next ln=ListNode(1) ln.add_to_the_end(2) ln.add_to_the_end(3) ln.add_to_the_end(4) ln.add_to_the_end(5) s.removeNthFromEnd(ln,5) ln.printLinkedList()

## Monday, March 12, 2018

### Beat 100% of python3 submissions

Yes! I did it.

Here is simple code that gives me performance described on picture above.

Here is simple code that gives me performance described on picture above.

class powxy: def pow(self, x, n): if n == 0.0: return 1 if n < 0: n = -n x = 1/x if n % 2 == 0: return self.pow(x*x, int(n/2)) return x*self.pow(x*x, int(n/2))

Subscribe to:
Posts (Atom)