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

Method addLists

java/Chapter 2/Question2_5/QuestionB.java:26–41  ·  view source on GitHub ↗
(LinkedListNode l1, LinkedListNode l2)

Source from the content-addressed store, hash-verified

24 }
25
26 private static LinkedListNode addLists(LinkedListNode l1, LinkedListNode l2) {
27 int len1 = length(l1);
28 int len2 = length(l2);
29 if (len1 < len2) {
30 l1 = padList(l1, len2 - len1);
31 } else {
32 l2 = padList(l2, len1 - len2);
33 }
34 PartialSum sum = addListsHelper(l1, l2);
35 if (sum.carry == 0) {
36 return sum.sum;
37 } else {
38 LinkedListNode result = insertBefore(sum.sum, sum.carry);
39 return result;
40 }
41 }
42
43 private static LinkedListNode padList(LinkedListNode l, int padding) {
44 LinkedListNode head = l;

Callers 1

mainMethod · 0.95

Calls 4

lengthMethod · 0.95
padListMethod · 0.95
addListsHelperMethod · 0.95
insertBeforeMethod · 0.95

Tested by

no test coverage detected