每日一题 为了工作 2020 0326 第二十四题

/**
*
* 问题:两个单链表相加生成相加链表
* 假设链表中每一个节点的值都在0-9之间, 那么链表整体就可以代表一个整数。
* 例如: 9->3->7, 可以代表整数 937。
* 给定两个这种链表的头节点 head1和 head2, 请生成代表两个整数相加值的结果链表。
* 例如: 链表 1为 9->3->7, 链表 2为 6->3, 最后生成新的结果链表为 1->0->0->0。
*
* 解答:
* 利用栈结构求解
*
* 1.将两个链表分别从左到右遍历, 遍历过程中将值压栈, 这样就生成了两个链表节点值的
* 逆序栈, 分别表示为 s1和 s2。例如: 链表 9->3->7, s1从栈顶到栈底为 7, 3, 9;
* 链表6->3, s2从栈顶到栈底为3, 6。
*
* 2.将 s1和 s2同步弹出, 这样就相当于两个链表从 低位到 高位依次弹出, 在这个过程中
* 生成相加链表即可, 同时需要关注每一步是否有进位, 用 ca表示。
* 例如: s1先弹出7, s2先弹出3, 这一步相加结果为10, 产生了进位, 令ca=1, 然后
* 生成一个节点值为 0 的新节点, 记为 new1; s1再弹出3, s2再弹出6, 这时进位为 ca=1,
* 所以这一步相加结果为 10, 继续产生进位, 仍令 ca=1, 然后生成一个节点值为 0的新节点,
* 记为 new2, 令 new2.next=new1; s1再弹出 9, s2为空, 这时 ca=1, 这一步相加结
* 果为10,仍令 ca=1, 然后生成一个节点值为 0的新节点, 记为 new3,令new3.next=new2。
* 这一步也是模拟简单的从低位到高位进位相加的过程。
*
* 3.当s1和s2都为空时, 还要关注一下进位信息是否为 1, 如果为 1, 比如步骤2中的例子,表示
* 还要生成一个节点值为 1的新节点, 记为 new4,令new4.next=new3。
*
* 4. 返回新生成的结果链表即可。
*
* @author 雪瞳
*
*/

* 代码

public class Node {
	
	public int value;
	public Node next;
	public Node(int data) {
		this.value=data;
	}
}

  

import java.util.Stack;

public class AddList {
	
	public Node addList(Node head1,Node head2) {
		Node current = null;
		Stack<Integer> stack1 = new Stack<>();
		Stack<Integer> stack2 = new Stack<>();
		//将链表元素存入栈内
		current=head1;
		while(current!=null) {
			stack1.push(current.value);
			current=current.next;
		}
		current=head2;
		while(current!=null) {
			stack2.push(current.value);
			current=current.next;
		}
		//遍历栈进行数据求和运算
		int ca = 0;
		int value = 0;
		int valueStack1=0;
		int valueStack2=0;
		current = null;
		Node newNode = null;
		while(!stack1.isEmpty()||!stack2.isEmpty()) {
			
			valueStack1=stack1.isEmpty()?0:stack1.pop();
			valueStack2=stack2.isEmpty()?0:stack2.pop();
			value = valueStack1+valueStack2+ca;
			if(value >= 10) {
				value = value-10;
				ca = 1;
			}
			current = newNode;
			newNode = new Node(value);
			newNode.next=current;
		}
		if(ca==1) {
			Node head = new Node(1);
			head.next=newNode;
			return head;
		}
		
		return newNode;
	}
}

  

import java.util.Random;
import java.util.Scanner;

public class TestAddList {


	public static void main(String[] args) {
		TestAddList test = new TestAddList();
		AddList add = new AddList();
		
		Scanner sc = new Scanner(System.in);
		int length1;
		System.out.println("请输入链表长度:...");
		length1 = sc.nextInt();
		int length2;
		System.out.println("请输入链表长度:...");
		length2 = sc.nextInt();
		Node head1;
		Node head2;
		head1=test.getNodeList(length1);
		head2=test.getNodeList(length2);
		
		System.out.println("初始状态...");
		test.showByTip(head1);
		test.showByTip(head2);
		
		Node addHead = add.addList(head1,head2);
		System.out.println("求和后状态...");
		test.showByTip(addHead);
		
	}
	public void showByTip(Node head) {
		Node current = null;
		System.out.println("链表内的元素顺序显示如下:...");
		current=head;
		while(current!=null) {
			System.out.print(current.value+"\t");
			current=current.next;
		}
		System.out.println("");
	}
	public Node getNodeList(int listLength) {
		Random rand = new Random();
		Node nodeList[]= new Node[listLength];
		for(int i=0;i<listLength;i++) {
			nodeList[i]= new Node(rand.nextInt(10)); 
		}
		for(int i=0;i<listLength-1;i++) {
			nodeList[i].next=nodeList[i+1];
		}
		return nodeList[0];
	}
}

  

*运行结果

 

 

 

 

posted @ 2020-03-26 13:06  雪瞳  阅读(184)  评论(0编辑  收藏  举报