从零开始的力扣(第十九天)~
1.两数相加
给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。
如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
您可以假设除了数字 0 之外,这两个数都不会以 0 开头。
示例:
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807
—————————————————————————————————————————
我是直接将新数组保存在l1内,然后再注意一下进位机制与剩余的不参与加法的位数
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
cur1 = l1
cur2 = l2
while cur1 and cur2:
cur1.val = cur1.val + cur2.val
if cur1.val >= 10:
cur1.val = cur1.val % 10
if cur1.next:
cur1.next.val += 1
else:
cur1.next = ListNode(1)
cur1 = cur1.next
cur2 = cur2.next
while cur1 and cur1.val == 10:
cur1.val = 0
if cur1.next:
cur1.next.val += 1
else:
cur1.next = ListNode(1)
cur1 = cur1.next
if cur2:
cur = l1
while cur.next:
cur = cur.next
cur.next = cur2
return l1
我觉得还可以通过将两个数不同位数的高位补0,这样就可以没有那么多判断条件了。
2.扁平化多级双向链表
您将获得一个双向链表,除了下一个和前一个指针之外,它还有一个子指针,可能指向单独的双向链表。这些子列表可能有一个或多个自己的子项,依此类推,生成多级数据结构,如下面的示例所示。
扁平化列表,使所有结点出现在单级双链表中。您将获得列表第一级的头部。
示例:
以上示例的说明:
给出以下多级双向链表:
我们应该返回如下所示的扁平双向链表:
—————————————————————————————————————————
双指针方法,将每次遍历到有child节点后的所有节点连接到child内部,遍历child个数次
"""
# Definition for a Node.
class Node(object):
def __init__(self, val, prev, next, child):
self.val = val
self.prev = prev
self.next = next
self.child = child
"""
class Solution(object):
def flatten(self, head):
"""
:type head: Node
:rtype: Node
"""
cur1 = cur2 = head
while cur2 is not None:
if cur1.child:
cur1 = cur1.next
cur2.next = cur2.child
cur2.next.prev = cur2
cur2.child = None
while cur2.next:
cur2 = cur2.next
if cur1:
cur2.next = cur1
cur2.next.prev = cur2
cur1 = cur2 = head
cur1 = cur1.next
cur2 = cur2.next
return head
3.复制带随机指针的链表
给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点。
要求返回这个链表的深拷贝。
示例:
提示:
你必须返回给定头的拷贝作为对克隆列表的引用。
—————————————————————————————————————————
通过哈希表,先拷贝主链表,再考虑random
"""
# Definition for a Node.
class Node(object):
def __init__(self, val, next, random):
self.val = val
self.next = next
self.random = random
"""
class Solution(object):
def copyRandomList(self, head):
"""
:type head: Node
:rtype: Node
"""
if not head:
return None
res = res_tail = Node(0,None,None)
tem = head
dict_tem = {}
while head:
res_tail.next = Node(head.val,None,None)
res_tail = res_tail.next
dict_tem[head] = res_tail
head = head.next
res = res.next
res_tail = res
head = tem
while head:
if head.random:
res.random = dict_tem[head.random]
head, res = head.next, res.next
res = res_tail
return res
4.旋转链表
给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数。
示例 1:
输入: 1->2->3->4->5->NULL, k = 2
输出: 4->5->1->2->3->NULL
解释:
向右旋转 1 步: 5->1->2->3->4->NULL
向右旋转 2 步: 4->5->1->2->3->NULL
示例 2:
输入: 0->1->2->NULL, k = 4
输出: 2->0->1->NULL
解释:
向右旋转 1 步: 2->0->1->NULL
向右旋转 2 步: 1->2->0->NULL
向右旋转 3 步: 0->1->2->NULL
向右旋转 4 步: 2->0->1->NULL
—————————————————————————————————————————
先判断链表的长度,再使用双指针,把链表末尾转到头就可以了,注意集中特殊情况
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def rotateRight(self, head, k):
"""
:type head: ListNode
:type k: int
:rtype: ListNode
"""
if not head:
return None
if not k:
return head
length = self.length(head)
step = length - k % length
cur1 = cur2 = head
for i in range(step - 1):
cur1 = cur1.next
while cur2.next:
cur2 = cur2.next
cur2.next = head
head = cur1.next
cur1.next = None
return head
def length(self, head):
cur = head
count = 0
while cur:
cur = cur.next
count += 1
return count
以上就是今日经验!