44 Views

0 Likes

In this lesson, Alvin explores the strategy to solving the following interview problem:

Write a function,

zipper_lists, that takes in the head of two linked lists as arguments. The function should zipper the two lists together into single linked list by alternating nodes. If one of the linked lists is longer than the other, the resulting list should terminate with the remaining nodes. The function should return the head of the zippered linked list.Do this

in-place, by mutating the original Nodes.You may assume that both input lists are non-empty.

```
a = Node("a")
b = Node("b")
c = Node("c")
a.next = b
b.next = c
# a -> b -> c
x = Node("x")
y = Node("y")
z = Node("z")
x.next = y
y.next = z
# x -> y -> z
zipper_lists(a, x)
# a -> x -> b -> y -> c -> z
```

```
a = Node("a")
b = Node("b")
c = Node("c")
d = Node("d")
e = Node("e")
f = Node("f")
a.next = b
b.next = c
c.next = d
d.next = e
e.next = f
# a -> b -> c -> d -> e -> f
x = Node("x")
y = Node("y")
z = Node("z")
x.next = y
y.next = z
# x -> y -> z
zipper_lists(a, x)
# a -> x -> b -> y -> c -> z -> d -> e -> f
```

```
s = Node("s")
t = Node("t")
s.next = t
# s -> t
one = Node(1)
two = Node(2)
three = Node(3)
four = Node(4)
one.next = two
two.next = three
three.next = four
# 1 -> 2 -> 3 -> 4
zipper_lists(s, one)
# s -> 1 -> t -> 2 -> 3 -> 4
```

If you need additional support taking these DSA skills and actually applying them, take Alvin's *complete* data structures and algorithms course on Structy. You can try out the concepts yourself in their interactive code editor and learn advanced DSA patterns like stack exhaustive recursion.

Use discount code **TARO20** to get 20% off the entire Structy DSA learning experience with the link here: structy.net

Follow Alvin on LinkedIn: https://www.linkedin.com/in/alvin-zablan-b73a92117/

Related Videos

ðŸ‘‘

0:48

DSA Crash Course [Part 87] - Outro

ðŸ‘‘

5:27

DSA Crash Course [Part 86] - Array Stepper Walkthrough

ðŸ‘‘

5:39

DSA Crash Course [Part 85] - Array Stepper Approach

ðŸ‘‘

6:31

DSA Crash Course [Part 84] - Counting Change Walkthrough

ðŸ‘‘

6:26

DSA Crash Course [Part 83] - Counting Change Approach

ðŸ‘‘

5:52

DSA Crash Course [Part 82] - Summing Squares Walkthrough

ðŸ‘‘

6:22

DSA Crash Course [Part 81] - Summing Squares Approach

ðŸ‘‘

8:00

DSA Crash Course [Part 80] - Non Adjacent Sum Walkthrough

ðŸ‘‘

8:33

DSA Crash Course [Part 79] - Non Adjacent Sum Approach

ðŸ‘‘

5:34

DSA Crash Course [Part 78] - Max Path Sum Walkthrough

Copyright Â© Taro