请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点。
示例:
输入: head = [4,5,1,9], node = 5
输出: [4,1,9]
思路: 以往删除node需要将node前一个结点指向下下一个结点即可。而此题直接传入删除的节点且是单向链表,无法找到node前一个节点。因此,转换一种思路,让此node取代下一个节点。虽然简单,但是打破传统思维。
代码:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteNode(self, node):
"""
:type node: ListNode
:rtype: void Do not return anything, modify node in-place instead.
"""
node.val, node.next = node.next.val, node.next.next
给你一个单链表的引用结点 head。链表中每个结点的值不是 0 就是 1。已知此链表是一个整数数字的二进制表示形式。请你返回该链表所表示数字的 十进制值 。
示例:
输入:head = [1,0,1]
输出:5
思路1: 遍历整个链表,把每个节点的值append到stack。最后遍历stack计算2进制转10进制的值。(想法太low了,主要是没想到下面思路2进制转10进制的做法)
思路2: 遍历整个链表时,res=res*2+node.val,得出10进制值。
代码:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def getDecimalValue(self, head: ListNode) -> int:
cur = head
stack = []
res = 0
while cur != None:
stack.append(cur.val)
cur = cur.next
length = len(stack)
for i in range(length):
if stack[length-1-i] == 0: continue
else: res += 2 ** i
return res
def getDecimalValue(self, head: ListNode) -> int:
cur = head
res = 0
while cur:
res = res*2 + cur.val
cur = cur.next
return res
实现一种算法,找出单向链表中倒数第 k 个节点。返回该节点的值。
示例:
输入: 1->2->3->4->5 和 k = 2
输出: 4
代码: 也可以采用下一题的双指针(快慢指针)
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def kthToLast(self, head: ListNode, k: int) -> int:
cur = head
res = []
while cur:
res.append(cur.val)
cur = cur.next
return res[-k]
输入一个链表,输出该链表中倒数第k个节点。为了符合大多数人的习惯,本题从1开始计数,即链表的尾节点是倒数第1个节点。例如,一个链表有6个节点,从头节点开始,它们的值依次是1、2、3、4、5、6。这个链表的倒数第3个节点是值为4的节点。
示例:
给定一个链表: 1->2->3->4->5, 和 k = 2.
返回链表 4->5.
思路1: 先遍历统计链表长度,记为n ;设置一个指针走 (n-k) 步,即可找到链表倒数第 k 个节点。返回的是链表
思路2: 使用双指针则可以不用统计链表长度。具体流程:
1、初始化: 前指针 former 、后指针 latter ,双指针都指向头节点 head 。
2、构建双指针距离: 前指针 former 先向前走 k 步(结束后,双指针 former 和 latter 间相距 k步)。
3、双指针共同移动: 循环中,双指针 former 和 latter 每轮都向前走一步,直至 former 走过链表尾节点时跳出(跳出后,latter 与尾节点距离为k−1,即 latter 指向倒数第 k 个节点)。
4、返回值: 返回 latter 即可。
代码:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def getKthFromEnd(self, head: ListNode, k: int) -> ListNode:
former, latter = head, head
for _ in range(k):
if not former: return
former = former.next
while former:
former, latter = former.next, latter.next
return latter
定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点。
示例:
输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
思路1: 迭代:通过遍历链表把值存储到栈,除最后一个不为空的节点。然后遍历链表依次pop()重绘链表。
思路2: 双指针:在每次构造一个新的节点对象反向指向前一个节点。相当于重新方向构造了一个链表。链表的头结点为None
代码:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# 思路1
def reverseList(self, head: ListNode) -> ListNode:
stack = []
cur = head
while cur:
stack.append(cur.val)
cur = cur.next
cur = head
while cur:
cur.val = stack.pop()
cur = cur.next
return head
# 思路2
def reverseList(self, head: ListNode) -> ListNode:
if not head:
return None
pre = head
cur = None
while pre:
node = ListNode(pre.val) # 创建节点
node.next = cur # 串联节点
cur = node
pre = pre.next
return cur
编写代码,移除未排序链表中的重复节点。保留最开始出现的节点。
示例:
输入:[1, 2, 3, 3, 2, 1]
输出:[1, 2, 3]
代码:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def removeDuplicateNodes(self, head: ListNode) -> ListNode:
if head == None:
return
vals = set()
vals.add(head.val) # 初始化vals为第一个节点的值
prev, cur = head, head.next # 快慢指针
while cur:
if cur.val in vals:
prev.next = cur.next
cur = cur.next
else:
vals.add(cur.val)
prev = cur
cur = cur.next
return head
输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
输出:Reference of the node with value = 8
思路: 长度A+B=B+A,同时将node1、node2分别从headA、headB出发,当A到达尾节点时,node1、node2分别从headB、headA出发。时间复杂度为 O ( n + m ) O(n+m) O(n+m)
代码:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
node1, node2 = headA, headB
while node1 != node2:
node1 = (node1.next if node1 else headB)
node2 = (node2.next if node2 else headA)
return node1
给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点。返回删除后的链表的头节点。
示例:
输入: head = [4,5,1,9], val = 5
输出: [4,1,9]
代码:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteNode(self, head: ListNode, val: int) -> ListNode:
if head.val == val:
return head.next
pre, cur = head, head.next
while cur:
if cur.val == val:
pre.next = cur.next # 指向下下一个节点
return head
cur = cur.next
pre = pre.next
将两个升序链表合并为一个新的升序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
示例:
输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4
代码: 递归
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
if not l1: return l2 # 终止条件,直到两个链表都空
if not l2: return l1
if l1.val <= l2.val: # 递归调用
l1.next = self.mergeTwoLists(l1.next,l2)
return l1
else:
l2.next = self.mergeTwoLists(l1,l2.next)
return l2
删除链表中等于给定值 val 的所有节点。
示例:
输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5
代码:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def removeElements(self, head: ListNode, val: int) -> ListNode:
while head and head.val == val:
head = head.next
pre = head
cur = head.next if head != None else head
while cur:
if cur.val == val:
pre.next = cur.next
cur = cur.next
else:
pre, cur = cur, cur.next
return head
请判断一个链表是否为回文链表。
示例:
输入: 1->2->2->1
输出: true
代码:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def isPalindrome(self, head: ListNode) -> bool:
stack = []
cur = head
while cur:
stack.append(cur.val)
cur = cur.next
return stack == stack[::-1] # [::-1]倒序
给定一个链表,判断链表中是否有环。
示例:
思路: 节点个数n,循环节点个数m。一个指针一次走两步,一个一次走一步,则:n+m和n+2m的时候,两个指针位置相同
代码:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def hasCycle(self, head: ListNode) -> bool:
if head == None or head.next == None:
return False
slow, fast = head, head.next
while slow != fast:
if fast == None or fast.next == None:
return False
slow = slow.next
fast = fast.next.next
return True