Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b6r9iSyHu5qSJMzH2n
40 lines
1.9 KiB
Plaintext
40 lines
1.9 KiB
Plaintext
Merge, remove n-th from end, palindrome, duplicates and intersection
|
|
====================================================================
|
|
|
|
Merge 1 -> 3 -> 5 and 2 -> 4 -> 6 -> 8 -> 9:
|
|
a: 1 -> 3 -> 5 -> null b: 2 -> 4 -> 6 -> 8 -> 9 -> null
|
|
compare 1 with 2 -> take 1 from a
|
|
merged so far: [1]
|
|
compare 3 with 2 -> take 2 from b
|
|
merged so far: [1, 2]
|
|
compare 3 with 4 -> take 3 from a
|
|
merged so far: [1, 2, 3]
|
|
compare 5 with 4 -> take 4 from b
|
|
merged so far: [1, 2, 3, 4]
|
|
compare 5 with 6 -> take 5 from a
|
|
merged so far: [1, 2, 3, 4, 5]
|
|
one list is empty; attach the rest of the other whole: 6 -> 8 -> 9 -> null
|
|
result: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 8 -> 9 -> null
|
|
|
|
Remove the n-th node from the end of 1 -> 2 -> 3 -> 4 -> 5:
|
|
n = 1 -> 1 -> 2 -> 3 -> 4 -> null
|
|
n = 2 -> 1 -> 2 -> 3 -> 5 -> null
|
|
n = 3 -> 1 -> 2 -> 4 -> 5 -> null
|
|
n = 4 -> 1 -> 3 -> 4 -> 5 -> null
|
|
n = 5 -> 2 -> 3 -> 4 -> 5 -> null
|
|
n = 4 on a 3-node list throws IllegalArgumentException: n = 4 is longer than the list
|
|
|
|
Palindrome check (the list is put back the way it was):
|
|
1 -> 2 -> 3 -> 2 -> 1 -> null -> true list afterwards: 1 -> 2 -> 3 -> 2 -> 1 -> null
|
|
1 -> 2 -> 2 -> 1 -> null -> true list afterwards: 1 -> 2 -> 2 -> 1 -> null
|
|
1 -> 2 -> 3 -> 4 -> null -> false list afterwards: 1 -> 2 -> 3 -> 4 -> null
|
|
7 -> null -> true list afterwards: 7 -> null
|
|
null -> true list afterwards: null
|
|
|
|
Remove duplicates from sorted 1 -> 1 -> 2 -> 3 -> 3 -> 3 -> 4 -> 4 -> null
|
|
-> 1 -> 2 -> 3 -> 4 -> null
|
|
|
|
Intersection: a = 4 -> 1 -> 8 -> 4 -> 5 -> null b = 5 -> 6 -> 1 -> 8 -> 4 -> 5 -> null (the 8 -> 4 -> 5 tail is one shared chain)
|
|
the lists join at the node holding 8; both 1s before it are different nodes with the same value
|
|
a list that merely has the same values 8 -> 4 -> 5 but its own nodes: intersection = null
|