用递归求单链表中值最大的节点
2013-06-15 16:35
225 查看
如题:
有某链表如下:
请用递归求链表值最大的节点,实现如下:
有某链表如下:
public class LinkNode { private int value; private LinkNode nextNode; }
请用递归求链表值最大的节点,实现如下:
package com; /** * 递归获取单链表值最大的节点 * * @author CaoZP * */ public class LinkNode { private int value; private LinkNode nextNode; public LinkNode() { } public LinkNode(int value) { this.value = value; } public LinkNode(int value, LinkNode nextNode) { this.value = value; this.nextNode = nextNode; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public LinkNode getNextNode() { return nextNode; } public void setNextNode(LinkNode nextNode) { this.nextNode = nextNode; } @Override public String toString() { return "LinkNode [value=" + value + ", nextNode=" + nextNode + "]"; } public void printNode() { StringBuilder str = new StringBuilder("["); LinkNode node = getNextNode(); while(node!=null) { str.append(node.getValue()).append(","); node = node.getNextNode(); } str.append("]"); System.out.println(str); } public static void main(String[] args) { LinkNode head = new LinkNode(123); LinkNode node = new LinkNode(156); LinkNode node1 = new LinkNode(0); LinkNode node2 = new LinkNode(456); LinkNode node3 = new LinkNode(-100); LinkNode node4 = new LinkNode(2); head.setNextNode(node); node.setNextNode(node1); node1.setNextNode(node2); node2.setNextNode(node3); node3.setNextNode(node4); head.printNode(); // LinkNode maxNode = getMaxNode(head); // System.out.println(maxNode); LinkNode minNode = getMinNode(head); System.out.println(minNode); } /** * 获取链表中值最大的节点 * * @param head * @return */ public static LinkNode getMaxNode(LinkNode head) { if(head ==null) { throw new NullPointerException("arg is null"); } //nextNode为空说明到了链尾 if(head.getNextNode()==null) { return head; } //当前节点的值大于nextNode把nextNode的nexNode设为head的nextNode if(head.getValue()>head.getNextNode().getValue()) { LinkNode childNextNode = head.getNextNode().getNextNode(); head.setNextNode(childNextNode); return getMaxNode(head); }else{ //否则就比较nextNode和nextNode的nextNode return getMaxNode(head.getNextNode()); } } /** * 获取链表中值最大的节点 * * @param head * @return */ public static LinkNode getMinNode(LinkNode head) { if(head ==null) { throw new NullPointerException("arg is null"); } //nextNode为空说明到了链尾 if(head.getNextNode()==null) { return head; }//0 15 56 -17 189 //当前节点的值大于nextNode把nextNode的nexNode设为head的nextNode if(head.getValue()<head.getNextNode().getValue()) { LinkNode childNextNode = head.getNextNode().getNextNode(); head.setNextNode(childNextNode); return getMinNode(head); }else{ //否则就比较nextNode和nextNode的nextNode return getMinNode(head.getNextNode()); } } }
相关文章推荐
- 关于在链表中用递归形式,求出链表中的最大值,及节点数和平均值的函数
- 某公司面试题,用递归求单链表中值最大的节点
- 递归从尾到头打印链表每个节点的值
- 65.输出单向链表中节点上最大值
- 二叉树中节点的最大距离(树的最长路径)——递归解法
- 【基础备忘】关于递归删除链表节点为什么不会断链问题解释
- 删除排序链表中重复的节点 递归方式
- 单链表中一趟遍历找出值最大节点
- 二叉树中相差最大的两个节点间的差的绝对值(层次遍历+递归先序)
- 在二进制树中的节点之间的最大距离(最长路径树)——递归解决方案
- 求链表最大节点的前驱结点
- 二叉查找树前驱、后继,非递归不使用栈不使用访问标记的中序遍历,最大最小节点
- 数据结构—链表-单链表应用-删除元素最大的节点
- 《编程之美》——求二叉树中节点的最大距离(非递归)
- leetcode-24,交换链表相邻节点,递归实现
- 《编程之美》——求二叉树中节点的最大距离(非递归)
- 二叉树中节点的最大距离(递归,动态规划)
- 求二叉树中节点的最大距离 | 递归
- 二叉树的最大路径和 II--要求开始于根节点--则 先加上根节点,再递归左右子树。
- 编程之美之求二叉树中节点的最大距离(递归和非递归法)