Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0c79727

Browse files
author
Ram swaroop
committed
maximum sum path: minor fix
1 parent aa2fc20 commit 0c79727

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/me/ramswaroop/linkedlists/MaximumSumLinkedList.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,25 @@ public static <E extends Comparable<E>> SingleLinkedNode<E> maximumSumLinkedList
4040
while (curr1 != null || curr2 != null) {
4141
// if either of the list runs out first
4242
if (curr1 == null) {
43+
// check whether we are in list 1 currently
44+
if (isList1) break;
45+
4346
node.next = curr2;
4447
node = node.next;
4548
curr2 = curr2.next;
4649
continue;
4750
}
4851
if (curr2 == null) {
52+
// check whether we are in list 2 currently
53+
if (!isList1) break;
54+
4955
node.next = curr1;
5056
node = node.next;
5157
curr1 = curr1.next;
5258
continue;
5359
}
5460

61+
// switch lists once both node values match
5562
if (curr1.item.compareTo(curr2.item) == 0) {
5663
isList1 = !isList1;
5764
}
@@ -76,6 +83,8 @@ public static void main(String a[]) {
7683
linkedList1.add(33);
7784
linkedList1.add(44);
7885
linkedList1.add(55);
86+
linkedList1.add(88);
87+
linkedList1.add(90);
7988
linkedList1.printList();
8089
SingleLinkedList<Integer> linkedList2 = new SingleLinkedList<>();
8190
linkedList2.add(12);
@@ -84,8 +93,6 @@ public static void main(String a[]) {
8493
linkedList2.add(33);
8594
linkedList2.add(34);
8695
linkedList2.add(67);
87-
linkedList2.add(88);
88-
linkedList2.add(90);
8996
linkedList2.printList();
9097
SingleLinkedList.printList(maximumSumLinkedList(linkedList1.head, linkedList2.head));
9198
}

0 commit comments

Comments
 (0)