题目描述1:链表中倒数第K个节点
输入一个链表,输出该链表中倒数第k个结点。
Python测试:
// An highlighted block
class Solution:
def FindKthToTail(self, head, k):
# write code here
if head == None or k <= 0:
return None
p1 = head
p2 = head
for i in range(k-1):
if p1.next == None:
return None
p1 = p1.next
while p1.next != None:
p1 = p1.next
p2 = p2.next
# print("p2: ", p2.val)
return p2
def getNewChart(self,list):
if list:
node = ListNode(list.pop(0))
node.next = self.getNewChart(list)
return node
class ListNode:
def __init__(self,x):
self.val = x
self.next = None
if __name__ == "__main__":
a = Solution()
num = [5,2,3,4,1,6,7,0,8]
listnode = a.getNewChart(num)
print(a.FindKthToTail(listnode, 2).val)
题目描述2:反转链表
输入一个链表,反转链表后,输出新链表的表头。
Python测试:
// An highlighted block
class Node:
def __init__(self,data=None,next = None):
self.data = data
self.next = next
def rev(link):
##将原链表的第一个节点变成了新链表的最后一个节点,同时将原链表的第二个节点保存在cur中
pre = link
cur = link.next
pre.next = None
while cur:
#从原链表的第二个节点开始遍历到最后一个节点,将所有节点翻转一遍
temp = cur.next
cur.next = pre
pre = cur
cur =temp
return pre
if __name__ == '__main__':
link = Node(1, Node(2, Node(3, Node(4, Node(5, Node(6, Node(7, Node(8, Node(9)))))))))
root = rev(link)
while root:
print(root.data)
root = root.next
以翻转第二个节点为例
temp = cur.next是将cur的下一个节点保存在temp中,也就是节点3,因为翻转后,节点2的下一个节点变成了节点1,原先节点2和节点3之间的连接断开,通过节点2就找不到节点3了,因此需要保存
cur.next = pre就是将节点2的下一个节点指向了节点1
然后pre向后移动到原先cur的位置,cur也向后移动一个节点,也就是pre = cur ,cur =temp
这就为翻转节点3做好了准备
题目描述3:合并两个排序的链表
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
Python测试:递归实现
// An highlighted block
class Solution:
# 返回合并后列表
def Merge(self, pHead1, pHead2):
# write code here
if not pHead1:
return pHead2
if not pHead2:
return pHead1
if pHead1.val < pHead2.val:
pres = pHead1
pres.next = self.Merge(pHead1.next, pHead2)
else:
pres = pHead2
pres.next = self.Merge(pHead1, pHead2.next)
return pres
def getNewChart(self,list):
if list:
node = ListNode(list.pop(0))
node.next = self.getNewChart(list)
return node
class ListNode:
def __init__(self,x):
self.val = x
self.next = None
if __name__ == '__main__':
a = Solution()
link1 = [2, 3, 4, 6, 7, 8]
link2 = [1, 6, 7, 8]
listnode1 = a.getNewChart(link1)
listnode2 = a.getNewChart(link2)
root = a.Merge(listnode1, listnode2)
while root:
print(root.val)
root = root.next
题目描述4:树的子结构
输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
Python测试:
// An highlighted block
class Solution:
# 给定两个二叉树(的根节点)A、B,判断B 是不是A 的二叉树
def HasSubtree(self, pRoot1, pRoot2):
if pRoot1 == None or pRoot2 == None:
return False
result = False
if pRoot1.val == pRoot2.val:
result = self.isSubtree(pRoot1, pRoot2)
if result == False:
result = self.HasSubtree(pRoot1.left, pRoot2) | self.HasSubtree(pRoot1.right, pRoot2)
return result
def isSubtree(self, root1, root2):
if root2 == None:
return True
if root1 == None:
return False
if root1.val == root2.val:
return self.isSubtree(root1.left, root2.left) & self.isSubtree(root1.right, root2.right)
return False
# 给定二叉树的前序遍历和中序遍历,获得该二叉树
def getBSTwithPreTin(self, pre, tin):
if len(pre)==0 | len(tin)==0:
return None
root = treeNode(pre[0])
for order,item in enumerate(tin):
if root .val == item:
root.left = self.getBSTwithPreTin(pre[1:order+1], tin[:order])
root.right = self.getBSTwithPreTin(pre[order+1:], tin[order+1:])
return root
class treeNode:
def __init__(self, x):
self.left = None
self.right = None
self.val = x
if __name__ == '__main__':
solution = Solution()
preorder_seq = [1, 2, 4, 7, 3, 5, 6, 8]
middleorder_seq = [4, 7, 2, 1, 5, 3, 8, 6]
treeRoot1 = solution.getBSTwithPreTin(preorder_seq, middleorder_seq)
preorder_seq = [1, 2, 3]
middleorder_seq = [2, 1, 3]
treeRoot2 = solution.getBSTwithPreTin(preorder_seq, middleorder_seq)
print(solution.HasSubtree(treeRoot1, treeRoot2))
总结:
链表中倒数第K个节点:https://blog.csdn.net/qq_38441207/article/details/88673798
反转链表:
https://blog.csdn.net/qq_38441207/article/details/88674093
合并两个排序的链表:
https://blog.csdn.net/qq_38441207/article/details/88679850
树的子结构
https://blog.csdn.net/qq_38441207/article/details/88680555