linked list values

test_00:

const a = new Node("a");
const b = new Node("b");
const c = new Node("c");
const d = new Node("d");
a.next = b;
b.next = c;
c.next = d;
// a -> b -> c -> dlinkedListValues(a); // -> [ 'a', 'b', 'c', 'd' ]

test_01:

const x = new Node("x");
const y = new Node("y");
x.next = y;// x -> ylinkedListValues(x); // -> [ 'x', 'y' ]

test_02:

const q = new Node("q");// qlinkedListValues(q); // -> [ 'q' ]

test_03:

linkedListValues(null); // -> [ ]

solutions

iterative

const linkedListValues = (head) => {
const values = [];
let current = head;
while (current !== null) {
values.push(current.val);
current = current.next;
}
return values;
};
  • n = number of nodes
  • Time: O(n)
  • Space: O(n)

recursive

const linkedListValues = (head) => {
const values = [];
_linkedListValues(head, values);
return values;
};
const _linkedListValues = (head, values) => {
if (head === null) return;
values.push(head.val);
_linkedListValues(head.next, values);
};
  • n = number of nodes
  • Time: O(n)
  • Space: O(n)

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to build a public anonymous chat app in Android using Pusher

Make a React Hook out of your Native Module

How to create OAuth authentication system with passport

Integrating App Index into your React Native application

Minimalist pre-renderer for React

ReactJS and React Native - Key Difference, Advantages, and Disadvantages

Setting Up Development Environment Using React Native on Windows

Visual Studio Code Website Screenshot for Download

Day 2 (100 Days of code)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
SunJet Liu

SunJet Liu

More from Medium

Man-made reasoning and Machine Learning simplified

Learning How to Code

Jacqueline Fernandez does pole dance with a twist on The Khatra Khatra Show; Karan Kundrra’s moves…

Jacqueline Fernandez does pole dance with a twist on The Khatra Khatra Show; Karan Kundrra's moves impress fans. Watch