How can the runner technique be used to arrange this linked list?

  • Thread starter Thread starter ainster31
  • Start date Start date
  • Tags Tags
    List
Click For Summary

Discussion Overview

The discussion revolves around the application of the runner technique to arrange a linked list, specifically addressing how two pointers (p1 and p2) can be utilized to find the midpoint of the list. The context includes a homework problem where participants explore the implications of the list's length being even or odd.

Discussion Character

  • Homework-related
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant expresses confusion about how p1 reaching the end of the linked list results in p2 being at the midpoint, using an example with an odd length (n=3).
  • Another participant clarifies that the list length is known to be even, suggesting that the initial example with n=3 is incorrect.
  • A subsequent post reiterates the requirement for n to be even, proposing that if n is odd, the linked list's effective length is even, as it is represented as 2n.
  • Further clarification is provided that while the linked list length is even, n can still be odd if it represents half the list length.

Areas of Agreement / Disagreement

Participants generally agree that the linked list length is even, but there is some confusion regarding the implications of n being odd or even in relation to the linked list's structure. The discussion remains unresolved regarding the specific application of the runner technique in these scenarios.

Contextual Notes

There are limitations in the assumptions made about the linked list's length and the definitions of n, which may affect the understanding of the runner technique's application.

ainster31
Messages
158
Reaction score
1

Homework Statement



POpQu.png


Homework Equations





The Attempt at a Solution



I don't understand how when p1 hits the end of the linked list, p2 will be at the midpoint.

This is how I am imagining it if n=3. Each step below represents an iteration.

1. a1 (p1, p2)->a2->a3->b1->b2->b3

2. a1->a2 (p2)->a3 (p1)->b1->b2->b3

3. a1->a2->a3 (p2)->b1->b2 (p1)->b3

4. Index out of bounds error because p2 now points to a node after b3.
 
Physics news on Phys.org
It says you don't know the list length but you do know that its length is even.

Hence you can't have n=3 in your thought experiment.

So if n=10 then:

i=0 p1=0 p2=2
i=1 p1=1 p2=4
i=2 p1=2 p2=6
i=3 p1=3 p2=8
i=4 p1=4 p2=0 // p2 is reset to the beginning of the list and p1 is at the midpoint
 
  • Like
Likes   Reactions: 1 person
Oh, so I guess they wanted n to be even. Even if n is odd, the linked list length is even since the length of the linkedlist is 2n.
 
ainster31 said:
Oh, so I guess they wanted n to be even. Even if n is odd, the linked list length is even since the length of the linkedlist is 2n.

Only the linked list length is even, if n is half the list length then n can be even or odd.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K