2.5问题:
给定两个用链表表示的数,每个结点包含一个数位。这些数位是反向存放的,也就是个位排在链表首部。编写函数对这两个整数求和,并用链表形式返回结果。
思考:递归思想,非常巧妙,需仔细分析思考,递归多次,每次都会创建一个节点,反馈给上一级,上一级的链表结点指针指向下一级创建的结点。当多级递归完成时,就会返回这个链表的首结点。完成运算。
import java.util.*;
//节点类
class Node {
protected Node next; //指针域
protected int data;//数据域
public Node( int data) {
this.data = data;
}
//显示此节点
public void display() {
System. out.print( data + " ");
}
}
//单链表
class LinkList {
public Node first; // 定义一个头结点
private int pos = 0;// 节点的位置
public LinkList() {
this.first = null;//this代表当前类
}
// 插入一个头节点
public void addFirstNode( int data) {
Node node = new Node(data);
node. next = first;
first = node;
}
// 删除一个头结点,并返回头结点
public Node deleteFirstNode() {
Node tempNode = first;
first = tempNode. next;
return tempNode;
}
// 在任意位置插入节点 在index的后面插入
public void add(int index, int data) {
Node node = new Node(data);
Node current = first;
Node previous = first;
while ( pos != index) {
previous = current;
current = current. next;
pos++;
}
node. next = current;
previous. next = node;
pos = 0;
}
// 删除任意位置的节点
public Node deleteByPos( int index) {
Node current = first;
Node previous = first;
while ( pos != index) {
pos++;
previous = current;
current = current. next;
}
if(current == first) {
first = first. next;
} else {
pos = 0;
previous. next = current. next;
}
return current;
}
// 根据节点的data删除节点(仅仅删除第一个)
public Node deleteByData( int data) {
Node current = first;
Node previous = first; //记住上一个节点
while (current. data != data) {
if (current. next == null) {
return null;
}
previous = current;
current = current. next;
}
if(current == first) {
first = first. next;
} else {
previous. next = current. next;
}
return current;
}
// 显示出所有的节点信息
public void displayAllNodes() {
Node current = first;
while (current != null) {
current.display();
current = current. next;
}
System. out.println();
}
// 根据位置查找节点信息
public Node findByPos( int index) {
Node current = first;
while( pos != index) {
current = current. next;
pos++;
}
pos = 0;
return current;
}
// 根据数据查找节点信息
public Node findByData( int data) {
Node current = first;
while (current. data != data) {
if (current. next == null)
return null;
current = current. next;
}
return current;
}
}
class addList{
public static void main(String args[]){
LinkList linkList1 = new LinkList();
linkList1.addFirstNode(1);//
linkList1.addFirstNode(2);//
linkList1.addFirstNode(3);//
linkList1.addFirstNode(4);//
linkList1.addFirstNode(5);//
linkList1.addFirstNode(6);//
System.out.println("l1 is :");
linkList1.displayAllNodes();
LinkList linkList2 = new LinkList();
linkList2.addFirstNode(1);//
linkList2.addFirstNode(2);//
linkList2.addFirstNode(3);//
linkList2.addFirstNode(4);//
linkList2.addFirstNode(5);//
linkList2.addFirstNode(6);//
System.out.println("l2 is :");
linkList2.displayAllNodes();
int carry=0;
LinkList linkList3 = new LinkList();
linkList3.first=addList(linkList1.first,linkList2.first,carry);
System.out.println("The result is:");
linkList3.displayAllNodes();
}
public static Node addList(Node l1,Node l2,int carry){
if(l1==null&&l2==null&&carry==0){
return null;
}
Node result=new Node(0);
int value = carry;
if(l1!=null){
value+=l1.data;
}
if(l2!=null){
value+=l2.data;
}
result.data=value%10;
Node more = addList(l1==null? null:l1.next, l2==null? null:l2.next, value>=10? 1:0);
result.next=more;
return result;
}
}
此程序有很多小技巧,例如该程序是怎么避免两个链表不等长的情况的,进位是怎么传递的。