1. ## another list question....

i have the following code:

Code:
```testNode *temp = current;
testNode *loc;

loc = current->next;
current->next = current->next->next;
loc->next = current;
current = loc;```
which swaps 2 nodes around (the first node be at the start of the list)

now i'm having trouble understanding how it works.

if we have the following:

BA and we want AB

Code:
```
loc = start->next; // we save A to loc
current->next = current->next->next; <--- i don't understand this bit! Since nothing
// exists after current, next, next. Does it just make the two nodes point together?
loc->next = current; // we put B after A
current = loc; // put A before B```
any ideas?
i just want to understand how it works.

2. Assuming u have 3 elements in ur list , say
1 -> 2 -> 3 -> NULL

and when u call ur function, curr is at the head of the list as u said,

so we have,
curr->val = 1
loc->val=2
((curr->next)->next)->val=3

now,
1) the node with value 3 is made the next node for the head node.
2) the next node for the value with value 2 is made curr ( which is ur head ) i.e, the node with value 2 is now ur head.

so, U have loc at the head of the list,

the new list wud be :
2 -> 1 -> 3 ->null

In ur case, we have at the start :
curr- >val = B
loc -> val = A
loc -> next =NULL;

" curr->next = (curr->next)->next "
curr->next now points to NULL,

next U do : " loc->next = curr "
so, loc is at the head of the list..
loc -> val = A
loc -> next = curr

curr->val = B
curr->next = NULL

3. what if there are only 2 nodes?

4. hmm.. and what does the last line

current = loc; do ?

5. it just makes "curr" point to ur "loc" which is the head of the list. Probably ur function returns curr back to the main program.

6. yes it does. ummm

Code:
```loc = current->next; // makes loc point to A
current->next = current->next->next; // sets B->next to null so we have B NULL A
loc->next = current; // we then get A NULL B
current = loc; // make "A" the head..```
based on my drawings etc inbetween AB there always appears to be NULL since the second step sets it to NULL and it doesnt change from there ??

i did another drawing this time it ends up being BAB

but at the last line when you make "current" point to "loc" it ends up being AB... is this correct? so actually a B is left behind

7. Do you need something like that?
Code:
```#include <stdio.h>
#include <stdlib.h>

typedef struct Node
{
int value;
struct Node *next;
}Node;

int main()
{
Node *current;
Node *temp;

current->value = 0;
current->next = malloc(sizeof(Node));
temp = current;
current= current->next;
current->value = 1 ;
current->next = temp;
current->next->next = NULL;
printf("%d %d",current->value, current->next->value);

return 0;
}```
Notice I don't free my memory this is crappy quality code.

8. nope, thanks anyway i have it working already. i was just trying to understand the code.

9. No,
> current->next = current->next->next; // sets B->next to null so we have B NULL A
that cannot be correct.
U r effectively splicing out A with this, U now have :

curr -> val = B, curr -> next =NULL
loc -> val =A , loc ->next =NULL

but ,U havent lost the node A as u still have a pointer to it.
u now make loc->next =NULL and re-insert it into the list at the head

so,
loc->val = A , loc ->next =curr
curr -> val = B , curr -> next = NULL

next u make curr point to loc ( head of the list) and return curr..

i did another drawing this time it ends up being BAB
at the last line when you make "current" point to "loc" it ends up being AB... is this correct? so actually a B is left behind

11. > so actually a B is left behind
do u mean u r losing the last B in ur list ? that is not possible either.. try reading what I had written for three nodes again. Its actually easy, just work it out on a piece of paper..

12. here's what i mean diagrammatically.. it might help explain easier:

http://img98.imageshack.us/img98/540/testzj4.jpg

13. I dont get that part where he says :

B -- NULL -- B
It deosnt make any sense. .
U have a doubt with two or three nodes now?

14. i have doubts about 2 nodes only.

well, loc->next = current;

whatevers in current (i.e. B) put it after loc->next.

since loc is at the position current->next

it makes B NULL B

15. Again ... A is "SPLICED OUT" of the list.
the next of current now points to NULL and not A.

for two nodes, at the start u have :

B -- > A --> NULL

B is ur curr and A is ur loc. right ?

now , after curr->next = (curr->next)->next

A is spliced out.but ,U still have "loc" pointing to A

so, U now have :

B -- > NULL in ur list
and A -- > NULL still exists , but not as a part of ur list..

even now,
B is ur curr , loc is ur A .. fine ??

after loc->next = curr U get :

A -- > B --> NULL