文章目录
* 代表高频题
一、反转链表 (206) *
反转一个单链表。
示例:
输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
cur, pre = head, None
while cur:
cur.next, pre, cur = pre, cur, cur.next
return pre
Note:python支持多变量一次性赋值,等号右边的值全部都保存了才会一次性赋值给等号左边的变量。
图解参考便于理解思路:https://blog.csdn.net/feliciafay/article/details/6841115
二、环形链表 (141) *
给定一个链表,判断链表中是否有环。
为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。
示例 1:
输入:head = [3,2,0,-4], pos = 1
输出:true
解释:链表中有一个环,其尾部连接到第二个节点。
示例 2:
输入:head = [1,2], pos = 0
输出:true
解释:链表中有一个环,其尾部连接到第一个节点。
示例 3:
输入:head = [1], pos = -1
输出:false
解释:链表中没有环。
方法一:快慢指针法
class Solution(object):
def hasCycle(self, head):
"""
:type head: ListNode
:rtype: bool
"""
ps = pf = head
while ps and pf and pf.next:
ps = ps.next
pf = pf.next.next
if ps == pf:
return True
return False
方法二:字典(较慢)
class Solution(object):
def hasCycle(self, head):
"""
:type head: ListNode
:rtype: bool
"""
dict = {}
p = head
while p:
if p in dict:
return True
else:
dict[p] = p.val
p = p.next
return False
三、回文链表 (234)
请判断一个链表是否为回文链表。
示例 1:
输入: 1->2
输出: false
示例 2:
输入: 1->2->2->1
输出: true
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def isPalindrome(self, head):
"""
:type head: ListNode
:rtype: bool
"""
l = []
while head:
l.append(head.val)
head = head.next
if l == l[::-1]:
return True
else:
return False
四、删除链表中的节点 (237)
请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点。
现有一个链表 – head = [4,5,1,9],它可以表示为: 4 -> 5 -> 1 -> 9
示例 1:
输入: head = [4,5,1,9], node = 5
输出: [4,1,9]
解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9.
示例 2:
输入: head = [4,5,1,9], node = 1
输出: [4,5,9]
解释: 给定你链表中值为 1 的第三个节点,那么在调用了你的函数之后,该链表应变为 4 -> 5 -> 9.
说明:
链表至少包含两个节点。
链表中所有节点的值都是唯一的。
给定的节点为非末尾节点并且一定是链表中的一个有效节点。
不要从你的函数中返回任何结果。
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def deleteNode(self, node):
"""
:type node: ListNode
:rtype: void Do not return anything, modify node in-place instead.
"""
node.val = node.next.val
node.next = node.next.next
Note:当前值被下一个结点值覆盖,当前指针指向下下结点。
五、删除排序链表中的重复元素 (83)
给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。
示例 1:
输入: 1->1->2
输出: 1->2
示例 2:
输入: 1->1->2->3->3
输出: 1->2->3
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
p = head
while p and p.next:
if p.val == p.next.val:
p.next = p.next.next
else:
p = p.next
return head
六、移除链表元素 (203)
删除链表中等于给定值 val 的所有节点。
示例:
输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def removeElements(self, head, val):
"""
:type head: ListNode
:type val: int
:rtype: ListNode
"""
pre, cur = None, head
while cur:
if cur.val != val:
pre, cur = cur, cur.next
else:
if pre:
pre.next = cur.next
else:
head = cur.next
cur = cur.next
return head
七、链表的中间结点 (876)
给定一个带有头结点 head 的非空单链表,返回链表的中间结点。
如果有两个中间结点,则返回第二个中间结点。
示例 1:
输入:[1,2,3,4,5]
输出:此列表中的结点 3 (序列化形式:[3,4,5])
返回的结点值为 3 。
示例 2:
输入:[1,2,3,4,5,6]
输出:此列表中的结点 4 (序列化形式:[4,5,6])
由于该列表有两个中间结点,值分别为 3 和 4,我们返回第二个结点。
提示:
给定链表的结点数介于 1 和 100 之间。
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def middleNode(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
ps = pf = head
while pf and pf.next:
ps = ps.next
pf = pf.next.next
return ps
Note:使用快慢两个指针,慢指针每次走1步,快指针每次走2步,当快指针移动到末尾时,慢指针刚好在中间结点。
八、合并两个有序链表 (21)
将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
示例:
输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4
# 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
"""
p = merge_list = ListNode(0)
while l1 and l2:
if l2.val > l1.val:
p.next, l1 = l1, l1.next
else:
p.next, l2 = l2, l2.next
p = p.next
p.next = l1 if l1 else l2
return merge_list.next
Note:merge_list = ListNode(0) 表示构造一个新链表的初始节点,p = merge_list 对其赋值,将merge_list保存以保留根节点,而p不断next传递节点关系;最后return merge_list.next,因为merge_list是根节点,merge_list.next才是头节点。