c - Connecting 2 singly linked lists by using a dummy node -
a little background info, doing huge project , part of merging 2 singly linked lists. linked lists both have different dummy node end , next field pointing node before, have access tail of list , able merge 2 lists in o(1) print both lists, try merge them, when print merged list, see 2 data adresses , cant find out why.
list1 = 500,501,502, list2 = 600,601, list3 = 500,501,502,address,adress,600,601
the node has *next field , int id field. dummynode *next shows last valid node of list1 , dummynode of list2 respectively board[i].ptr , board[k].ptr starting points of each list
and here code:
board[i].dummynode->next->next = board[k].ptr; free(board[i].dummynode); board[i].dummynode= board[k].dummynode; node * u = board[i].ptr; while(u!=board[k].dummynode) { printf("%d ",u->id); u = u->next; }
my guess board[i].dummynode->next
wasn't pointing @ tail of board[i]
. can print board[i].dummynode->next->id
before combine 2 lists?
if guess right, traverse tail of old dummy node, whatever pointing at, , head of k.
i0 -> i1 -> i2 -> dummy -> random k0 -> k1 -> k2 -> k dummy
becomes
i0 -> i1 -> i2 -> dummy -> random -> k0 -> k1 -> k2 -> k dummy
Comments
Post a Comment