Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why iteration linking works that way in Linked List

I cannot understand fully solution of 19th problem on Leetcode.

So when we created fast and slow – we created link of head? As I can judge yes. But then we iterate with fast – why it does not change head? I mean on every step we replace fast with fast.next? So somehow it doesn’t effect head and slow.

And then we replace slow.next with slow.next.next and it does effect head.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Could you please describe how it doesn’t replace nothing on first part and replace in the second.

var removeNthFromEnd = function(head, n) {
    let fast = head, slow = head

    for (let i = 0; i < n; i++) fast = fast.next // why it doesn’t change head and slow?

    if (!fast) return head.next

    while (fast.next) fast = fast.next, slow = slow.next
    slow.next = slow.next.next // why it change head?
    return head
 };

>Solution :

Assigning something to a variable will not mutate an object.

Assigning something to a property will mutate the object whose property you are setting.

In your case, fast is a variable, so assigning something to it like for example fast.next, will never mutate an object. It merely changes what fast is referencing.

On the other hand, slow.next is a property. Assigning something to that property will mutate the object that slow references.

Let’s visualise what fast = fast.next does. Let’s assume fast is the second node in a list of 3:

Before:

 head            fast 
  ↓               ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ data: B   β”‚   β”‚ data: G   β”‚   β”‚ data: D   β”‚
β”‚ next: ──────> β”‚ next: ──────> β”‚ next: nullβ”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

After:

 head                            fast 
  ↓                               ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ data: B   β”‚   β”‚ data: G   β”‚   β”‚ data: D   β”‚
β”‚ next: ──────> β”‚ next: ──────> β”‚ next: nullβ”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Now look what slow.next = slow.next.next does. Let’s assume slow references the first node:

Before:

 slow
 head
  ↓  
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ data: B   β”‚   β”‚ data: G   β”‚   β”‚ data: D   β”‚
β”‚ next: ──────> β”‚ next: ──────> β”‚ next: nullβ”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

After:

 slow
 head
  ↓  
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ data: B   β”‚   β”‚ data: G   β”‚   β”‚ data: D   β”‚
β”‚ next: ─────┐  β”‚ next: ──────> β”‚ next: nullβ”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β”Œ> β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
             β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading