Solutions to some linked list exercises.

Insert a node into a linked list

Insert in order Given a linked list of integers sorted from smallest (at the head end) to largest, and a pointer to a single node containing an integer, insert the node in the linked list so that it remains sorted.

This solution uses pointer to links.

Insert a node into a linked list

Insert in order Given a linked list of integers sorted from smallest (at the head end) to largest, and a pointer to a single node containing an integer, insert the node in the linked list so that it remains sorted.

This is the same problem as the previous, but uses a different technique.

This version does not use pointers to links.

Thus inserting at the head is a special case.

Cumulative sum

Cumulative sum Given a null-terminated linked list, in, create a new null-terminated linked, list out, of the same length, such that node i of out contains the sum of the data in in's nodes up to and including node i of list in. Detect heap exhaustion and report it by setting a boolean variable.

Delete the last node

Delete last node Given a nonempty list, delete the last node and set the new last link to null.

Deal

Deal Given a null terminated linked list, rearrange its nodes into two lists: <first node, third node, fifth node, ...> and <second node, fourth node, sixth node, ...>. Do not allocate any new nodes.


© Theodore S. Norvell 1996--2004.