剑指offer——代码的鲁棒性

面试题22:链表中倒数第k个节点

  • 题目描述:

输入一个链表,输出该链表中倒数第k个结点。

  • 详细代码:
# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def FindKthToTail(self, head, k):
        # write code here
		if not head or k <= 0:
			return None
			
		ahead = head
		behind = None

		for i in range(k - 1):
			if ahead.next:
				ahead = ahead.next
			else:
				return None
		
		behind = head
		while ahead.next:
			ahead = ahead.next
			behind = behind.next

		return behind

面试题23:链表中环的入口节点

  • 题目描述:

如果一个链表中包含环,如何找出环的入口节点?

  • 详细代码:
# -*- coding:utf-8 -*-  
class ListNode:  
    def __init__(self, x):  
        self.val = x  
        self.next = None  
class Solution:  
    def EntryNodeOfLoop(self, pHead):
    	pFast = pHead
    	pSlow = pHead

		while pFast and pFast.next:
			pFast = pFast.next.next
			pSlow = pSlow.next
			if pFast == pSlow:
				break

			if pFast == 0 or pFast.next == 0:
				return None
			pFast = pHead
			while pFast != pSlow:
				pFast = pFast.next
				pSlow = pSlow.next

			return pFast

面试题24:反转链表

  • 题目描述:

输入一个链表,反转链表并输出反转后链表的头节点。

  • 详细代码:
# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
    # 返回ListNode
    def ReverseList(self, pHead):
        # write code here
		ReversedHead = None
		pNode = pHead
		pPrev = None

		while pNode:
			pNext = pNode.next
			if not pNext:
				return pNode
			pNode.next = pPrev
			pPrev = pNode
			pNode = pNext

		return ReversedHead

面试题25:合并两个排序的链表

  • 题目描述:

输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。

  • 详细代码:
# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
    # 返回合并后列表
    def Merge(self, pHead1, pHead2):
        # write code here
        if not pHead1:
        	return pHead2
		if not pHead2:
			return pHead1

		MergedHead = None
		if  pHead1.val < pHead2.val:
			MergedHead = pHead1
			MergedHead.next = self.Merge(pHead1.next, pHead2)
		else:
			MergedHead = pHead2
			MergedHead.next = self.Merge(pHead1, pHead2.next)

		return MergedHead

面试题26:树的子结构

  • 题目描述:

输入两棵二叉树A和B,判断B是不是A的子结构。

  • 详细代码:
# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    def HasSubtree(self, pRoot1, pRoot2):
        # write code here
        result = False
        if pRoot1 and pRoot2:
        	if pRoot1.val == pRoot2.val:
        		result = self.HasSubtreeCore(pRoot1, pRoot2)
        	if not result:
        		result = self.HasSubtree(pRoot1.left, pRoot2)
        	if not result:
        		result = self.HasSubtree(pRoot1.right, pRoot2)
        return result

	def HasSubtreeCore(self, pRoot1, pRoot2):
		if not pRoot2:
			return True
		if not pRoot1:
			return False
		if pRoot1.val != pRoot2.val:
			return False
		return self.HasSubtreeCore(pRoot1.left, pRoot2.left) and self.HasSubtreeCore(pRoot1.right, pRoot2.right)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值