Kodr

Kodr

  • Content
  • Blog
  • About
  • Languages iconEnglish
    • Español

›Problems: Linked Lists

Technical Interviews

  • What are technical interviews based on?
  • Essential content & how to solve algorithms

Big-O Notation

  • Big-O Notation
  • Big-O Examples
  • Big-O Exercises

Optimisation Techniques

  • BUD optimisation
  • DIY Optimisation

Key Concepts

  • Linked Lists
  • Stack & Queues
  • Trees
  • Graphs
  • Tries
  • Sorting
  • Searching
  • Recursion
  • Dynamic Programming

Problems: Arrays & Strings

  • Is Unique
  • String Rotation
  • Check Permutation
  • URLify
  • One Edit Away
  • String Compression
  • Rotate Matrix
  • Zero Matrix
  • Valid Parenthesis

Problems: Linked Lists

  • Remove duplicates
  • Kth to last node
  • Delete Middle Node
  • Partition
  • Sum List
  • Palindrome
  • Intersection
  • Loop Detection

Problems: Stacks & Queues

  • Stack Min
  • Stack Of Plates
  • Queue via Stacks
  • Sort Stack
  • Animal Shelter

Problems: Trees

  • Path Sum
  • Construct Binary Tree from Inorder and Postorder Traversal

Problems: Binary Search Trees & Graphs

  • Route Between Two Nodes
  • Minimal Tree
  • List of Depths
  • Check Balanced
  • Validate BST
  • Successor
  • Build Order
  • First Common Ancestor
  • Check Sub-Tree
  • (Harder) Random Node

Problems: Sorting & Searching

  • Merge Sorted Array
  • First Bad Version

Problems: Dynamic Programming

  • Triple Step
  • Magic Index
  • (Hard) Towers Of Hanoi

Delete Middle Node

Implement an algorithm to delete a node in the middle (Le., any node but the first and last node, not necessarily the exact middle) of a singly linked list, given only access to that node.

👉 Link here to the repo to solve the problem

imagen

👌 Tips

Picture the list 1 -> 5 -> 9 -> 12. Removing 9 would make it look like 1 -> 5 -> 12. You only have access to the 9 node. Can you make it look like the correct answer?

👊 Solution 1

This problem has surprisingly a fairly easy solution. We want to check if the Node passed is not null (the list is not empty) or that this is the last node. All we really want to do is shorten the list based on the pivot node passed (the deletion node). So we want to temporarily assign a new node to the next one and transfer all the values of the next node to this current node that we want to "delete". Then we want to assign this node.next with the next node which we hold temporarily.

public static boolean deleteMiddleNode(SingleLinkedListNode singleLinkedListNode) {
    if (singleLinkedListNode == null || singleLinkedListNode.next == null) return false;
    SingleLinkedListNode next = singleLinkedListNode.next;
    singleLinkedListNode.data = next.data;
    singleLinkedListNode.next = next.next;
    return true;
}

Question borrowed from “Cracking the coding interview”

Last updated on 3/6/2020
← Kth to last nodePartition →
  • 👉 Link here to the repo to solve the problem
  • 👌 Tips
  • 👊 Solution 1
Kodr
Content
IntroBig O NotationOptimisation
Coding Problems
Check PermutationIsUniqueURLify
Blogs
Tecnica Pomodoro
Social
About MeGitHub
Follow @diego_romero_x
Copyright © 2021 Kodr