用JAVA語言鍊錶實現兩個整數相加?

時間 2021-06-01 01:31:22

1樓:

說實話,如果是最低位在鍊錶頭部,那這道題其實不應該算作medium難度。

解法如下:

class

Solutionif(

l2!=

null

)cur

.next

=new

ListNode

(carry%10

);carry

/=10

;cur

=cur

.next;}

return

dummy

.next;}

}LeetCode Medium: 2. Add Two Numbers

若最高位在鍊錶頭部,則可以使用以下解法:

先把兩個鍊錶逆置相加再逆置

使用棧非常規解法:先求兩個鍊錶長度,再使用遞迴

2樓:wuxinliulei

你指的是這個題目嗎?

Youare

given

twonon

-empty

linked

lists

representing

twonon

-negative

integers

.The

digits

arestored

inreverse

order

andeach

oftheir

nodes

contain

asingle

digit

.Add

thetwo

numbers

andreturn

itas

alinked

list

.You

mayassume

thetwo

numbers

donot

contain

anyleading

zero

,except

thenumber

0itself

.case1

:Input:(2

->4->3)

+(5->6->4)

Output:

7->0->8case2

:Input:(5

->7->3)

+(6->4)

Output:

1->2->1這個鍊錶相加相對簡單;順序加,然後進製就行了;

如果不是reverse儲存的,而是正序儲存的,就比較複雜了,就需要先翻轉鍊錶,然後再相加,然後再翻轉了。

提供這兩種情況的實現;

/*** 代表乙個逆序的數字鍊錶

*/class

AddLinkedList

implements

Cloneable

static

class

Node

public

final

intnumber

;public

Node

nextNode;}

public

AddLinkedList

reverse

(final

AddLinkedList

targetLinkedList

)return

reverseAddLinkedList;}

/*** 正序儲存的鍊錶相加,先翻轉,再相加;

* 看需求要返回的是正序還是反序,反序的話再呼叫一下reverse就行

* * @param addLinkedList

* @return

* @throws CloneNotSupportedException

*/public

AddLinkedList

addSequence

(final

AddLinkedList

addLinkedList

)throws

CloneNotSupportedException

/*** 反序儲存的鍊錶相加

* * @param addLinkedList

* @return

* @throws CloneNotSupportedException

*/public

AddLinkedList

addReverse

(final

AddLinkedList

addLinkedList

)throws

CloneNotSupportedException

/*** 反序儲存相加

* * @param firstAddLinkedList

* @param secondAddLinkedList

* @return

*/private

AddLinkedList

addSequence

(final

AddLinkedList

firstAddLinkedList

,final

AddLinkedList

secondAddLinkedList)if

(l2!=null)p

.nextNode

=new

Node

(sum%10

);sum

=sum/10

;p=p

.nextNode;}

final

Node

newHeadNode

=listNode

.nextNode

;listNode

=null

;final

AddLinkedList

resultAddLinkedList

=new

AddLinkedList

();resultAddLinkedList

.headNode

=newHeadNode

;return

resultAddLinkedList;}

@Override

protected

Object

clone

()throws

CloneNotSupportedException

catch

(CloneNotSupportedExceptione)

if(this

.headNode

==null

)// Put clone into "virgin" state

clone

.headNode

=new

Node

(headNode

.number

);Node

tempNode

=clone

.headNode

;// Initialize clone with our elements

for(

Nodee=

headNode

.nextNode;e

!=null;e

=e.nextNode

)return

clone;}}

這是出了什麼問題 可以實現頭插法鍊錶,就是出現了後面的問題。

迴圈鍊錶初始化時有問題,你一開始構建head節點時就應該扣成乙個單節點的環。然後add方法也要改一下,新節點加入後要斷開原來的連線置NULL。自己拿張紙畫一下就明白了。 鍊錶的最後的位址貌似沒有賦予數值 NULL。到 while p 出現問題。好像,不確定,沒有仔細看。21 5 2019 20 31...

實現《演算法導論》中的習題,用什麼語言比較好

姚鋼強 首先明確的是如果你還不熟悉任何一門程式語言,看這本書適不適合你的。因為演算法在沒有程式設計能力的前提下就是廢物。所以用你熟悉的語言去寫這些演算法,目的是學習演算法,而不是糾結於語言。 孫立 我當年是用Turbo Pascal練的,我覺得即使今天應該也還是乙個不錯的選擇。依我看用資料結構比較簡...

能否用兩塊arduino系列的板子實現資訊交流?

滄狼 GSM,ESP8266,用串列埠的,或者EC20,L610用USB的,單純用無線就不大可能了,這麼遠的距離使用網路合適,但是網路需要一方是公網IP並且埠開啟,或者連線到乙個伺服器。 邱廖鈞 用Arduino控制的硬體通常通訊距離不會有多遠的,最常用的藍芽通常也就在10公尺範圍內進行通訊。相距八...