let's go ahead and solve this together
i want you to take 2 linkedlists as inputs
example: [2, 5 , 8] + [1 , 0 , 1] returns [ 9, 5, 3]
reverse both of them and add them
return the reversed linked list
in java
let's go ahead and solve this together
i want you to take 2 linkedlists as inputs
example: [2, 5 , 8] + [1 , 0 , 1] returns [ 9, 5, 3]
reverse both of them and add them
return the reversed linked list
in java
(reverse (map #'+ '(2 5 8) '(9 5 3)))
>in java
no
alright alright i'll start us with a Node class..
public class Node (){
Node nextNode;
int value;
public Node(Node nextNode, int value){
this.nextNode = nextNode;
this value = value;
}}
create two strings
read each int from the lists, append to respective string
convert each string to Long or BigInteger
add
create linkedlist from resulting number and return
main runner class...
public static void main (String[] args){
Node input1 = new Node(2, new Node(5, new Node(8, null));
Node input2 = new Node(1, new Node(0, new Node(1, null));
}
we're gunna need a method that takes an input and reverses them, yo...
who wants to try for this
public Node reverse (Node node){
Node reversed = node;
//reverse me pls
return reversed;
}
alright let's modify our Node class just a little to add a method that points to the next node..
public class Node (){
Node nextNode;
int value;
public Node(Node nextNode, int value){
this.nextNode = nextNode;
this.value = value;
}
public Node next(){
return this.nextNode;
}
}
Baby's first leetcode
In Haskell, this is just:
type Digit = Int -- 0 to 9
type Carry = Int -- 0 or 1
addRev :: [Digit] -> [Digit] -> Carry -> [Digit] -> [Digit]
addRev (d1 : n1) (d2 : n2) c acc =
let s = d1 + d2 + c in
addRev n1 n2 (s `div` 10) (s `mod` 10 : acc)
addRev (d1 : n1) [] c acc = addRev (d1 : n1) [0] c acc
addRev [] (d2 : n2) c acc = addRev [0] (d2 : n2) c acc
addRev [] [] 0 acc = acc
addRev [] [] 1 acc = (1 : acc)
homework :: [Digit] -> [Digit] -> [Digit]
homework n1 n2 = add n1 n2 0 []
main = print $ homework [2, 5, 8] [1, 0, 1] -- [9, 5, 3]
homework n1 n2 = addRev n1 n2 0 []
how do you do the white background thing
You need a 4channel gold account.
which dialect?
fucking tell me you bitch
You need a Any Forums gold account
read the sticky post you retard
TELL ME RIGHT NOW YOU SHIT im dead fucking serious
Do the needful sir
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode head = null;
ListNode current = null;
boolean carry = false;
while(l1 != null || l2 != null) {
var l1Digit = l1 == null ? 0 : l1.val;
var l2Digit = l2 == null ? 0 : l2.val;
var digitSum = l1Digit + l2Digit + ((carry) ? 1 : 0);
if(digitSum >= 10) {
digitSum -= 10;
carry = true;
} else {
carry = false;
}
var nextDigit = new ListNode(digitSum);
if(head == null) {
head = nextDigit;
current = head;
} else {
current.next = nextDigit;
current = nextDigit;
}
l1 = l1 == null ? null : l1.next;
l2 = l2 == null ? null : l2.next;
}
if(carry) {
current.next = new ListNode(1);
}
return head;
}
Gold Accounts can access sticky. We do not cheat the system here.
FUCK YOU BRO YOU TELL ME RIGHT NOW OR KICK UR FUCKING COMPUTER DOWN
>no
Good answer.
Say "No" To Java.
The Sticky Says:
>To use the Code tag, book-end your body of code with: and