学习目标:
1. 链表理论基础:了解一下链接基础。
2. 203.移除链表元素:理解 虚拟头结点 的使用技巧。
3. 707.设计链表:考察 链表综合操作的题目,不算容易,可以练一练 使用虚拟头结点。
4. 206.反转链表:反转链表需要注意创建临时指针。
学习内容:
链表理论基础
学习产出:
- 笔记 2 条
1. 刷leetcode的时候,链表的节点都默认定义好了,直接用就行了。题外需要自己手写链表,Python定义链表节点方式如下:
class ListNode:
def __init__(self, val, next=None):
self.val = val
self.next = next
2. 依据双指针的思路,206.反转链表(版本二)递归法写法如下:
(版本二)递归法
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
return self.reverse(head, None)
def reverse(self, cur: ListNode, pre: ListNode) -> ListNode:
if cur == None:
return pre
temp = cur.next
cur.next = pre
return self.reverse(temp, cur)