MCPcopy Index your code
hub / github.com/careercup/ctci / addLists

Method addLists

java/Chapter 2/Question2_5/QuestionA.java:8–30  ·  view source on GitHub ↗
(
			LinkedListNode l1, LinkedListNode l2, int carry)

Source from the content-addressed store, hash-verified

6public class QuestionA {
7
8 private static LinkedListNode addLists(
9 LinkedListNode l1, LinkedListNode l2, int carry) {
10 if (l1 == null && l2 == null && carry == 0) {
11 return null;
12 }
13
14 LinkedListNode result = new LinkedListNode();
15 int value = carry;
16 if (l1 != null) {
17 value += l1.data;
18 }
19 if (l2 != null) {
20 value += l2.data;
21 }
22 result.data = value % 10;
23 if (l1 != null || l2 != null) {
24 LinkedListNode more = addLists(l1 == null ? null : l1.next,
25 l2 == null ? null : l2.next,
26 value >= 10 ? 1 : 0);
27 result.setNext(more);
28 }
29 return result;
30 }
31
32 public static int linkedListToInt(LinkedListNode node) {
33 int value = 0;

Callers 1

mainMethod · 0.95

Calls 1

setNextMethod · 0.95

Tested by

no test coverage detected