文章目录
- day1 链表中倒数第k个节点
- day2 合并两个排序的链表
day1 链表中倒数第k个节点
链接
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def getKthFromEnd(self, head, k):
"""
:type head: ListNode
:type k: int
:rtype: ListNode
"""
former,latter = head,head
for _ in range(k):
former = former.next
while former:
former,latter = former.next,latter.next
return latter
参考大佬,实在是妙这种
day2 合并两个排序的链表
链接
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
cur = dum = ListNode(0)
while l1 and l2:
if l1.val < l2.val:
cur.next,l1 = l1,l1.next
else:
cur.next,l2 = l2,l2.next
cur = cur.next
cur.next = l1 if l1 else l2
return dum.next
越发现自己的菜了