Algorithm做算法题,Review点评英文文章,Tip总结技术技巧,Share做技术分享。每周打卡一次,这就是ARTS打卡。
# 1. 做算法题
# LeetCode 206. 反转链表 (opens new window)
题目:
反转一个单链表。
示例:
输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
2
解题思路:
循环遍历链表,将每个节点p的指针赋给p.next.next节点,边界处理需要原头节点指向NULL,返回新的头节点head。链表的操作最容易出错的地方是丢失指针,画出图来整个过程就清晰了。边界处理也容易出错,引入一个“哨兵”节点在头节点前面。
原始链表是这样的:

增加“哨兵节点”:

开始迭代,第一步保存head.next指针到next中

第二步head指向pre

第三步pre右移

第四步head右移

一次迭代完成,下一次迭代得到:

直到head.next为NULL时迭代终止:

解题代码:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
if head is None:
return head
pre_p = None
while head.next is not None:
next_p = head.next
head.next = pre_p
pre_p = head
head = next_p
head.next = pre_p
return head
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 2. 点评英文文章
阅读《Concise Guide to Databases》 (opens new window)第三章,介绍了数据库是如何物理存储的,以及数据库操作时如何执行的。从原理上解释了数据库底层机制,但并不高深难懂。
# 3. 技术技巧
Eclipse中代码补全功能可以避免输入较长内容而不慎写错,使用部分代码+Ctrl+Alt+/即可补全,例如输入pr+Ctrl+Alt+/得到关键字private。
# 4. 技术分享
终于完成vuepress博客配置和部署,访问地址 (opens new window)。配置过程参考此文 (opens new window),配置过程中侧边导航栏配置有点复杂,可以参考官网文档 (opens new window)。注意本地跑起来后,放在vercel上build,配置命令参数为:yarn global add vuepress && yarn init -y && yarn docs:build,输出目录设置为:/docs/.vuepress/dist。以后写博客简单了,直接上传markdown文件到github,再修改一下侧边栏导航的配置即可,具体配置参数见我的github (opens new window)。