Android面试问题总结——编程题篇

1. 删除链表中的重复结点。

/**
 * 输入:2, 3, 5, 8, 3, 7, 8, 9, 8, 9, 10
 * 输出:2, 3, 5, 7, 8, 9, 10
 */
public class TestClass {
    public static void main(String[] args) {
        // 构造单链表
        int[] num = { 2, 3, 5, 8, 3, 7, 8, 9, 8, 9, 10 };

        ListNode head = new ListNode(num[0]);
        ListNode temp = head;

        for (int i = 1; i < num.length; i++) {
            ListNode node = new ListNode(num[i]);
            temp.next = node;
            temp = node;
        }
        // 构造单链表结束

        // 输出链表
        printList(head);

        // 链表排序
        head = bubbleSort(head);
        // 输出链表
        printList(head);

        // 删除重复结点
        head = deleteDuplicate(head);
        // 输出链表
        printList(head);
    }

    private static ListNode bubbleSort(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        
        ListNode cur = null;
        ListNode tail = null;
        cur = head;
        
        while(cur.next != tail){
            while (cur.next != tail) {
                if(cur.data > cur.next.data){
                    int temp = cur.data;
                    cur.data = cur.next.data;
                    cur.next.data = temp;
                }
                cur = cur.next;
            }
            tail = cur;
            cur = head;
        }
            
        
        return head;
    }

    private static ListNode deleteDuplicate(ListNode head) {
        ListNode cur = head;
        if (cur == null) {
            return head;
        }
        while (cur.next != null) {
            if (cur.data == cur.next.data) {
                cur.next = cur.next.next; // 当前值与下一个值相等, 则删除
            } else {
                cur = cur.next;
            }
        }
        return head;
    }

    /**
     * 遍历输出链表
     * @param head 头结点
     */
    private static void printList(ListNode head) {
        ListNode temp = head;
        if (temp == null) {
            return;
        }
        temp.display();
        while (temp.next != null) {
            temp.next.display();
            temp = temp.next;
        }
        System.out.println();
    }
}

class ListNode {
    int data;
    ListNode next;

    public ListNode(int data) {
        this.data = data;
    }
    
    public ListNode getNext(){
        return next;
    }
    
    public void display(){
        System.out.print(data + ", ");
    }
}
 

2. 返回二叉树中某结点的所有祖先结点

/**
 * 返回二叉树中某结点的所有祖先结点
 *       13
 *       /    \
 *    8     99
 *   / \    /
 *  6  10  54
 *      \
 *    7
 */
public class TestClass2 {
    public static void main(String[] args) {
        int[] content = new int[] { 13, 8, 6, 7, 10, 99, 54 };

        BinaryTree tree = new BinaryTree(content);
        tree.preOrder(tree.root);
        System.out.println();
        tree.printAncestors(tree.root, 7);
    }
}

/**
 * 二叉树结点
 *
 */
class TreeNode {
    int data;
    TreeNode leftChild;
    TreeNode rightChild;

    public TreeNode(int data) {
        this.data = data;
        this.leftChild = null;
        this.rightChild = null;
    }
}

class BinaryTree {
    TreeNode root;

    public BinaryTree(int[] data) {
        for (int i = 0; i < data.length; i++) {
            addNode(data[i]);
        }
    }

    private void addNode(int data) {
        TreeNode current = root;
        if (root == null) {
            root = new TreeNode(data);
            return;
        }

        while (true) {
            if (data < current.data) {
                if (current.leftChild == null) {
                    current.leftChild = new TreeNode(data);
                    return;
                } else {
                    current = current.leftChild;
                }
            } else {
                if (current.rightChild == null) {
                    current.rightChild = new TreeNode(data);
                    return;
                } else {
                    current = current.rightChild;
                }
            }
        }
    }

    public void preOrder(TreeNode node) {
        if (node != null) {
            System.out.print("[" + node.data + "] ");
            preOrder(node.leftChild);
            preOrder(node.rightChild);
        }
    }


    public void floorOrder(TreeNode node){
        if (node == null){
            return;
        }
        LinkedList<TreeNode> list = new LinkedList<>();
        list.add(root);
        
        TreeNode current;
        while (!list.isEmpty()) {
            current  = list.poll();
            System.out.print(current.data + ", ");
            if(current.leftChild != null){
                list.add(current.leftChild);
            }
            if(current.rightChild != null){
                list.add(current.rightChild);
            }
        }
    }

    public boolean printAncestors(TreeNode node, int value) {
        if (node == null) {
            return false;
        }
        if (node.data == value) {
            return true;
        }

        if (printAncestors(node.leftChild, value) || printAncestors(node.rightChild, value)) {
            System.out.print(node.data + "   ");
            return true;
        }
        return false;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值