1.Reverse Nodes in k-Group
将链表按K个进行分组,然后对每一个分组逆序
通过cur记住分组链表头,cur.next=head
则依次让第二个指向第一个,第三个指向第二个。。。
然后将cur.next指向新组的头,并将原来的头作为尾指向下一个分组,代码如下:
while countnode:
count += 1
if count < k:
countnode = countnode.next
else :
countnode = countnode.next
first = cur.next
tmp1 = first
while count > 1:
count -= 1
tmp = cur.next.next
#print tmp.val
cur.next.next = tmp.next
#print first.val
tmp.next = first
first = tmp
cur.next = first
cur = tmp1
cur.next = countnode
count = 0
tmp在进行定位时采用cur定位,而没有采用first是因为first还要用于作为tmp的指向进行更新。
2.Divide Two Integers
在不使用乘法和除法的前提下做除法,是的减法
为了提高性能,移位
while dividend >= divisor:
if dividend >= sub:
dividend -= sub
res += c
sub <<= 1
c <<= 1
else:
sub >>= 1
c >>= 1