File tree Expand file tree Collapse file tree 1 file changed +9
-2
lines changed
src/me/ramswaroop/linkedlists Expand file tree Collapse file tree 1 file changed +9
-2
lines changed Original file line number Diff line number Diff line change @@ -40,18 +40,25 @@ public static <E extends Comparable<E>> SingleLinkedNode<E> maximumSumLinkedList
40
40
while (curr1 != null || curr2 != null ) {
41
41
// if either of the list runs out first
42
42
if (curr1 == null ) {
43
+ // check whether we are in list 1 currently
44
+ if (isList1 ) break ;
45
+
43
46
node .next = curr2 ;
44
47
node = node .next ;
45
48
curr2 = curr2 .next ;
46
49
continue ;
47
50
}
48
51
if (curr2 == null ) {
52
+ // check whether we are in list 2 currently
53
+ if (!isList1 ) break ;
54
+
49
55
node .next = curr1 ;
50
56
node = node .next ;
51
57
curr1 = curr1 .next ;
52
58
continue ;
53
59
}
54
60
61
+ // switch lists once both node values match
55
62
if (curr1 .item .compareTo (curr2 .item ) == 0 ) {
56
63
isList1 = !isList1 ;
57
64
}
@@ -76,6 +83,8 @@ public static void main(String a[]) {
76
83
linkedList1 .add (33 );
77
84
linkedList1 .add (44 );
78
85
linkedList1 .add (55 );
86
+ linkedList1 .add (88 );
87
+ linkedList1 .add (90 );
79
88
linkedList1 .printList ();
80
89
SingleLinkedList <Integer > linkedList2 = new SingleLinkedList <>();
81
90
linkedList2 .add (12 );
@@ -84,8 +93,6 @@ public static void main(String a[]) {
84
93
linkedList2 .add (33 );
85
94
linkedList2 .add (34 );
86
95
linkedList2 .add (67 );
87
- linkedList2 .add (88 );
88
- linkedList2 .add (90 );
89
96
linkedList2 .printList ();
90
97
SingleLinkedList .printList (maximumSumLinkedList (linkedList1 .head , linkedList2 .head ));
91
98
}
You can’t perform that action at this time.
0 commit comments