剑指Offer-专题-代码鲁棒性

代码的鲁棒性

剑指Offer-专题在线编程

链表中倒数第k个结点

分析

我在这里使用了栈,本题主要注意几个问题

  • 输入的链表为空
  • k为0
  • k的值大于链表长度

代码

import java.util.Stack;
public class Solution {
    public ListNode FindKthToTail(ListNode head,int k) {
        if ( head == null || k == 0 ){
            return null;
        }
        int length = 0;
        Stack<ListNode> stack = new Stack();
        ListNode node = head;
        while( node != null ){
            stack.push(node);
            node = node.next;
            length++;
        }
        
        if( k > length ) 
            return null;
        
        for( int i = 1; i < k; i++ ){
            stack.pop();
        }
        return stack.pop();
    }
}

反转链表

分析

反转链表一直绕的头晕,这次看着大佬的代码,画图理解,终于有些头绪

代码

public class Solution {
    public ListNode ReverseList(ListNode head) {
        if( head == null || head.next == null ){
            return head;
        }

        ListNode pre = null;
        ListNode next = null;

        while( head != null ){
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        return pre;
    }
}

合并两个排序链表

分析

数据结构中很常见的题目

设置head和一个哨兵cur,对比两个链表的结点通过哨兵将结点连接到head上。

代码

public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
        if ( list1 == null && list2 == null ) return null;
        
        if ( list1 == null && list2 != null ) return list2;
        
        if ( list1 != null && list2 == null ) return list1;
        
        ListNode head = new ListNode(0);
        ListNode cur = head;
        
        while( list1 != null && list2 != null ){
            if ( list1.val <= list2.val ){
                cur.next = list1;
                list1 = list1.next;
            }else{
                cur.next = list2;
                list2 = list2.next;
            }
            cur = cur.next;
        }
        
        if ( list1 != null ) cur.next = list1;
        if ( list2 != null ) cur.next = list2;
        
        return head.next;
    }
}

树的子结构

分析

这题需要注意一下子树与子结构的区别(能力有限感觉网上解释不太好,待找到好的解释再来补上)

代码

/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    public boolean HasSubtree(TreeNode root1,TreeNode root2) {
        if ( root1 == null || root2 == null ) return false;
        
        return subTree(root1, root2)
            || subTree(root1.left, root2)
            || subTree(root1.right, root2);
    }
    
    public boolean subTree ( TreeNode root1, TreeNode root2 ){
        if ( root2 == null ) return true;
        
        if ( root1 == null ) return false;
        
        if ( root1.val != root2.val ) 
            return subTree(root1.left, root2) 
                || subTree(root1.right, root2);
        else
            return subTree(root1.left, root2.left)
                && subTree(root1.right, root2.right);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值