Recursion day 1

Principle of Recursion

  1. A simple base case (or cases) — a terminating scenario that does not use recursion to produce an answer.
  2. A set of rules, also known as recurrence relation that reduces all other cases towards the base case.

344. Reverse String

Write a function that reverses a string. The input string is given as an array of characters char[].

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

You may assume all the characters consist of printable ascii characters.

Example 1:

Input: ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]

Example 2:

Input: ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]

24. Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head.

You may not modify the values in the list’s nodes. Only nodes itself may be changed.

Example 1:

Input: head = [1,2,3,4]
Output: [2,1,4,3]

Example 2:

Input: head = []
Output: []

Example 3:

Input: head = [1]
Output: [1]


  • The number of nodes in the list is in the range [0, 100].
  • 0 <= Node.val <= 100

Practice how to convert For Loop to RECURSION

Sum- make a sum of string composed with numbers

ex. ‘123’

Recurrence Relation

  • recurrence relation: the relationship between the result of a problem and the result of its subproblems.
  • base case: the case where one can compute the answer directly without any further recursion calls. Sometimes, the base cases are also called bottom cases, since they are often the cases where the problem has been reduced to the minimal scale, i.e. the bottom, if we consider that dividing the problem into subproblems is in a top-down manner.

206. Reverse Linked List

Reverse a singly linked list.


Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL

Follow up:

A linked list can be reversed either iteratively or recursively. Could you implement both?





Consistency achieves everything

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

Recommended from Medium

Update: Upcoming Features

Transform Arrays With .reduce()

Simple modular GraphQl schemas and resolvers

3 Steps to Make Any Node.JS API Library Better

The Front-End Test Pyramid: How to Rethink Your Testing

🇸🇪 MySwedish fluency bits #57, Att stå på egna ben

Mocking and Stubbing with Cypress — Beginner to Advanced

Get the Language Code for an HTML Document in Node.JS

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
Joo Hee Paige Kim

Joo Hee Paige Kim

Consistency achieves everything

More from Medium

Level Order Traversal of Binary Tree Using Iteration

An Introduction to Linked Lists

Algorithms for Beginners

Understanding (luv’n) JavaScript thru Data Structures & Algorithms (DS & A)