Solutions using pointer algorithms in C for common Linked Lists problems.
The problems use the C language syntax, so they require a basic understanding of C and its pointer syntax. The emphasis is on the important concepts of pointer manipulation and linked list algorithms rather than the features of the C language.
A base class - linkedlist.c
, with basic utitlity functions, is defined in /libs.
All of the linked list code in this document uses the "classic" singly linked list structure:
- A single head pointer points to the first node in the list. Each node contains a single .next pointer to the next node.
- The .next pointer of the last node is NULL. The empty list is represented by a NULL head pointer.
- All of the nodes are allocated in the heap.
For a few of the problems, the solutions present the temporary "dummy node" or a "reference pointer" variation. They use memory in unusual ways, so they are a nice way to see if you really understand what's going on ๐
The problems roughly in order of difficulty:
- Count occurences of value i in linked list - Count.c
- Remove head and return value - Pop.c
- Get Nth value from linked list - GetNth.c
- Move head from source list to destination - moveHead.c
- Reverse a list - ReverseList.c
- Delete linked list and free memory - DeleteList.c
- Insert value at position N in linked list - InsertNth.c
- Recursively reverse a list - RecursiveReverse.c