141环形链表:
def hasCycle(self, head):
"""
:type head: ListNode
:rtype: bool
"""
n=head
x=0
if n==None:
return False
while n.next!=None:
n=n.next
x+=1
if x>10000:
return True
return False
21合并两个有序链表:
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
if l1 is None:
return l2
elif l2 is None:
return l1
elif l1.val < l2.val:
l1.next = self.mergeTwoLists(l1.next, l2)
return l1
else:
l2.next = self.mergeTwoLists(l1, l2.next)
return l2
203一处链表元素:
def removeElements(self, head, val):
"""
:type head: ListNode
:type val: int
:rtype: ListNode
"""
n=ListNode(-1)
c=n
n.next=head
while n.next:
if n.next.val==val:
n.next=n.next.next
else:
n=n.next
return c.next