《剑指offer》刷题笔记(代码的鲁棒性):链表中倒数第k个结点

《剑指offer》刷题笔记(代码的鲁棒性):链表中倒数第k个结点



前言

所谓的鲁棒性是指程序能够判断输入是否符合规范要求,并对不合要求的输入予以合理的处理。

提高代码的鲁棒性的有效途径是进行防御性编程。防御性编程是一种变成习惯,是指预见在什么地方可能会出现问题,并为这些可能出现的问题制定处理方式。比如试图打开文件时发现文件不存在,我们可以提示用户检查文件名和路径等等,这样当异常发生时,软件的行为也尽在我们的掌握之中,而不至于出现不可预见的事情。

比如下面这道题,“链表中倒数第K个结点”,这里隐含着一个条件就是链表中结点的个数大于k。我们想,如果链表中的结点的数目不是大于k个,那么代码会出什么问题?这样的思考方式能够帮助我们发现潜在的问题并提前解决问题。

题目描述

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

解题思路

定义两个指针,第一个指针从链表的头指针开始遍历向前走k-1,第二个指针保持不变;从第K步开始,第二个指针也是从链表的头指针开始遍历。由于两个指针的距离保持在k-1,当第一个指针到达链表的伪结点时,第二个指针正好是倒数第k个结点。

C++版代码实现

鲁棒性低

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
            val(x), next(NULL) {
    }
};*/
class Solution {
public:
    ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {

        ListNode* pAhead = pListHead;
        ListNode* pBehind = NULL;

        for(unsigned int i = 0; i < k - 1; ++i){
            pAhead = pAhead->next;
        }

        pBehind = pListHead;
        while(pAhead->next != NULL){
            pAhead = pAhead->next;
            pBehind = pBehind->next;
        }
        return pBehind;
    }
};

鲁棒性高

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
            val(x), next(NULL) {
    }
};*/
class Solution {
public:
    ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
        if(pListHead == NULL || k == 0)
            return NULL;

        ListNode* pAhead = pListHead;
        ListNode* pBehind = NULL;

        for(unsigned int i = 0; i < k - 1; ++i){
            if(pAhead->next != NULL)
                pAhead = pAhead->next;
            else
                return NULL;
        }

        pBehind = pListHead;
        while(pAhead->next != NULL){
            pAhead = pAhead->next;
            pBehind = pBehind->next;
        }
        return pBehind;
    }
};

Python 代码实现

鲁棒性差

# -*- 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
        pAhead = head
        pBehind = None

        for i in xrange(0, k-1):
            pAhead = pAhead.next
            return None

        pBehind = head
        while pAhead.next != None:
            pAhead = pAhead.next
            pBehind = pBehind.next

        return pBehind

鲁棒性高

# -*- 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

        pAhead = head
        pBehind = None

        for i in xrange(0, k-1):
            if pAhead.next != None:
                pAhead = pAhead.next
            else:
                return None

        pBehind = head
        while pAhead.next != None:
            pAhead = pAhead.next
            pBehind = pBehind.next

        return pBehind

更快

# -*- 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
        l=[]
        while head != None:
            l.append(head)
            head = head.next
        if k > len(l) or k < 1:
            return
        return l[-k]

系列教程持续发布中,欢迎订阅、关注、收藏、评论、点赞哦~~( ̄▽ ̄~)~

完的汪(∪。∪)。。。zzz

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值