如题:
有某链表如下:
1 public class LinkNode { 2 3 private int value; 4 5 private LinkNode nextNode; 6 }
请用递归求链表值最大的节点,实现如下:
1 package com; 2 3 /** 4 * 递归获取单链表值最大的节点 5 * 6 * @author CaoZP 7 * 8 */ 9 public class LinkNode { 10 11 private int value; 12 13 private LinkNode nextNode; 14 15 public LinkNode() { 16 17 } 18 19 20 21 public LinkNode(int value) { 22 this.value = value; 23 } 24 25 26 public LinkNode(int value, LinkNode nextNode) { 27 this.value = value; 28 this.nextNode = nextNode; 29 } 30 31 32 public int getValue() { 33 return value; 34 } 35 36 public void setValue(int value) { 37 this.value = value; 38 } 39 40 41 public LinkNode getNextNode() { 42 return nextNode; 43 } 44 45 public void setNextNode(LinkNode nextNode) { 46 this.nextNode = nextNode; 47 } 48 49 50 @Override 51 public String toString() { 52 return "LinkNode [value=" + value + ", nextNode=" + nextNode + "]"; 53 } 54 55 public void printNode() 56 { 57 StringBuilder str = new StringBuilder("["); 58 LinkNode node = getNextNode(); 59 while(node!=null) 60 { 61 str.append(node.getValue()).append(","); 62 node = node.getNextNode(); 63 } 64 str.append("]"); 65 System.out.println(str); 66 } 67 68 69 public static void main(String[] args) { 70 71 LinkNode head = new LinkNode(123); 72 LinkNode node = new LinkNode(156); 73 LinkNode node1 = new LinkNode(0); 74 LinkNode node2 = new LinkNode(456); 75 LinkNode node3 = new LinkNode(-100); 76 LinkNode node4 = new LinkNode(2); 77 78 head.setNextNode(node); 79 node.setNextNode(node1); 80 node1.setNextNode(node2); 81 node2.setNextNode(node3); 82 node3.setNextNode(node4); 83 84 head.printNode(); 85 // LinkNode maxNode = getMaxNode(head); 86 // System.out.println(maxNode); 87 LinkNode minNode = getMinNode(head); 88 System.out.println(minNode); 89 90 } 91 92 93 94 /** 95 * 获取链表中值最大的节点 96 * 97 * @param head 98 * @return 99 */ 100 public static LinkNode getMaxNode(LinkNode head) { 101 if(head ==null) 102 { 103 throw new NullPointerException("arg is null"); 104 } 105 //nextNode为空说明到了链尾 106 if(head.getNextNode()==null) 107 { 108 return head; 109 } 110 //当前节点的值大于nextNode把nextNode的nexNode设为head的nextNode 111 if(head.getValue()>head.getNextNode().getValue()) 112 { 113 LinkNode childNextNode = head.getNextNode().getNextNode(); 114 head.setNextNode(childNextNode); 115 return getMaxNode(head); 116 }else{ 117 //否则就比较nextNode和nextNode的nextNode 118 return getMaxNode(head.getNextNode()); 119 } 120 } 121 /** 122 * 获取链表中值最大的节点 123 * 124 * @param head 125 * @return 126 */ 127 public static LinkNode getMinNode(LinkNode head) { 128 if(head ==null) 129 { 130 throw new NullPointerException("arg is null"); 131 } 132 //nextNode为空说明到了链尾 133 if(head.getNextNode()==null) 134 { 135 return head; 136 }//0 15 56 -17 189 137 //当前节点的值大于nextNode把nextNode的nexNode设为head的nextNode 138 if(head.getValue()<head.getNextNode().getValue()) 139 { 140 LinkNode childNextNode = head.getNextNode().getNextNode(); 141 head.setNextNode(childNextNode); 142 return getMinNode(head); 143 }else{ 144 //否则就比较nextNode和nextNode的nextNode 145 return getMinNode(head.getNextNode()); 146 } 147 } 148 }