您的位置:首页 > 其它

LeetCode(2) Add Two Numbers

2017-12-31 21:31 423 查看
题目
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

example

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.


翻译一下:现在给你两个非空的链表,表示两个非负整数。数字以相反的顺序存储,每个节点包含一个数字。将两个数字相加,并将其作为链表返回。

知识点1—LinkedList
LinkedList类是List接口的实现类,所以说他具备List集合的特性,可以根据索引来随机访问集合中的元素。

他还实现了Deque接口,可以被当成双端队列使用,因此既可以当做“栈”,也可以当成队列

他内部是以链表的形式来保存集合中的元素的,因此随机访问集合元素时性能较差,但在插入、删除元素时性能比较出色

知识点2—链表
被数据结构摧残了两次,今天就来真正用一用。

链表是一种数据结构,和数组同级,ArrayList和linkedlist都是list的实现类,但是ArrayList底层实现是数组。下边着重介绍一下单向链表(因为这个例子中就用到了单向链表):

单向链表的就像是火车,所有节点串成一列,而且指针所指方向一样。每个列表中每个数据除了要存储原本的数据,还必须存储下一个数据的存储地址



例如:列表A={1,2,3,4,5,6}



JAVA代码实现
模拟节点:

public class Node {

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


自己封装一个LinkedList

public class LinkedList {
private Node first;
private Node last;

//判空的方法
public boolean isEmpty(){
return first==null;
}

//插入的方法
public void insert(int data){
Node newNode=new Node(data);
if(this.isEmpty()){
first=newNode;
last=newNode;
}
else{
last.next=newNode;
last=newNode;
}
}

//打印的方法
public void print(){
Node current=first;
String a="";
while(current!=null){
a=a+current.data+"->";
current=current.next;
}
System.out.println(a.substring(0, a.length()-2));
}

public Node getLast(){;
return this.last;
}

public Node getFirst(){
return this.first;
}
}


客户端调用:

public class Test {

public static void main(String[] args)
{
LinkedList list1=new LinkedList();
list1.insert(2);
list1.insert(4);
list1.insert(3);

LinkedList list2=new LinkedList();
list2.insert(5);
list2.insert(6);
list2.insert(4);
LinkedList list3=addTwoList(list1,list2);
list3.print();
}

public static LinkedList addTwoList(LinkedList list1,LinkedList list2){
LinkedList list3=new LinkedList();
//第一步,list1中的节点分别是2-4-3,得到的结果要是342
int num1=getNum(list1);
//获得list2的数字465
int num2=getNum(list2);
//两者相加:807
int num3=num1+num2;
System.out.println(num3);

//将807逆序放入list3中,打印一下(7-0-8),返回
int i=807/100%10;

list3.insert(num3/100%10);
list3.insert(num3/10%10);
list3.insert(num3%10);

return list3;
}

public static int getNum(LinkedList list){
//第一步,list1中的节点分别是2-4-3,得到的结果要是342
Node current=list.getFirst();
int sum=0;
int i=0;
while(current!=null){
if(i==0){
sum+=current.data;
}
else if(i==1){
sum+=current.data*10;
}
else if(i==2){
sum+=current.data*100;
}
i++;
current=current.next;
}
System.out.println(sum);
return sum;
}

}




网上给出的方案
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode(0);
ListNode p = l1, q = l2, curr = dummyHead;
int carry = 0;
while (p != null || q != null) {
int x = (p != null) ? p.val : 0;
int y = (q != null) ? q.val : 0;
int sum = carry + x + y;
carry = sum / 10;
curr.next = new ListNode(sum % 10);
curr = curr.next;
if (p != null) p = p.next;
if (q != null) q = q.next;
}
if (carry > 0) {
curr.next = new ListNode(carry);
}
return dummyHead.next;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: