Thursday, 15 September 2011

data structures - Understanding linked lists (Java) -



data structures - Understanding linked lists (Java) -

can please tell me if correct? studying midterm.

x variable pointing linked-list node , not lastly node on list. t points new node not in list.

x.next = t; t.next = x.next;

i believe when comes time update t.next, x.next no longer original node next x, instead t itself. create cycle in list

t = x.next x = t;

i believe nil list.

thank in advance!!

in case store node in temp variable. won't create cycle.

object temp = x.next; x.next = t; t.next = temp;

first have list this..

x--->y----->z-->

you want insert node t after x

right t is

t---->null

step 1- have temp pointing x's next

x---->y----->z-----> ^ | temp--

step 2- x's next pointing t

x----->t---->

now main list this

temp---->y---->z---->

step 3- t's next pointing temp next pointer

temp---->y--->z----> ^ | ---------- | x---->t---

so resulting list is

x--->t---->y---->z----->

java data-structures linked-list

No comments:

Post a Comment