Reverse Every K Nodes - c

Every k nodes form a segment. If the last few nodes are less than K, then you can ignore them.
Write a reverseKnodes() which reserves every segment in the linked list.
The function prototype is given as follow: void reversekNodes(ListNode** head, int k);
Input format:
The 1st line is the k
The 2nd line is the data to create the linked list and ends with a non-digit symbol Example:
Input: 3 1 2 3 4 5 6 7 8 9 10 a
Output: 3 2 1 6 5 4 9 8 7 10
#include <stdio.h>
#include <stdlib.h>
struct _listNode
{
int item;
struct _listNode *next;
};
typedef struct _listNode ListNode;
void printList (ListNode * head);
void deleteList (ListNode ** ptrHead);
void reverseKNodes (ListNode ** head, int K);
int
main ()
{
ListNode *head = NULL, *temp;
int i = 0;
int K = 0;
scanf ("%d", &K);
while (scanf ("%d", &i))
{
if (head == NULL)
{
head = (ListNode *) malloc (sizeof (ListNode));
temp = head;
}
else
{
temp->next = (ListNode *) malloc (sizeof (ListNode));
temp = temp->next;
}
temp->item = i;
}
temp->next = NULL;
reverseKNodes (&head, K);
printList (head);
deleteList (&head);
return 0;
}
void
printList (ListNode * head)
{
while (head != NULL)
{
printf ("%d ", head->item);
head = head->next;
}
printf ("\n");
}
void
deleteList (ListNode ** ptrHead)
{
ListNode *cur = *ptrHead;
ListNode *temp;
while (cur != NULL)
{
temp = cur->next;
free (cur);
cur = temp;
}
*ptrHead = NULL;
}
void
reverseKNodes (ListNode ** head, int K)
{
struct _listNode *reverse (struct _listNode *head, int k){
if (!head)
return NULL;
struct _listNode* cur = head;
struct _listNode* next = NULL;
struct _listNode* prev = NULL;
int count = 0;
while (cur != NULL && count < K)
{
next = cur->next;
cur->next = prev;
prev = cur;
cur = next;
count++;
}
if (next != NULL)
head->next = reverse(next, K);
return prev;
}
}
I am not allowed to change anything else other than the void function for reverseKNodes and I know I have done it wrong but I dont know where I went wrong. Could someone help me please?

A few issues with nested functions ...
Nested functions aren't standard C.
Are of limited benefit.
They add extra overhead to invoke.
So [please] don't use them.
And, your usage of one is incorrect. In reverseKNodes, you define reverse but never call it. So, reverseKNodes is a no-op.
So, to fix, move reverse out of the body of reverseKNodes and place it above. Then, actually invoke it in reverseKNodes:
void
reverseKNodes(ListNode **head, int K)
{
*head = reverse(*head, K);
}
Here is the full refactored code. For aid in debug (e.g. with gdb), I added code to allow the program to take an input file as an argument.
#include <stdio.h>
#include <stdlib.h>
struct _listNode {
int item;
struct _listNode *next;
};
typedef struct _listNode ListNode;
void printList(ListNode *head);
void deleteList(ListNode **ptrHead);
void reverseKNodes(ListNode **head, int K);
int
main(int argc,char **argv)
{
ListNode *head = NULL, *temp;
int i = 0;
int K = 0;
--argc;
++argv;
FILE *xf;
do {
if (argc != 1) {
xf = stdin;
break;
}
xf = fopen(*argv,"r");
if (xf != NULL)
break;
perror(*argv);
exit(1);
} while (0);
fscanf(xf,"%d", &K);
while (fscanf(xf,"%d", &i)) {
if (head == NULL) {
head = (ListNode *) malloc(sizeof(ListNode));
temp = head;
}
else {
temp->next = (ListNode *) malloc(sizeof(ListNode));
temp = temp->next;
}
temp->item = i;
}
temp->next = NULL;
reverseKNodes(&head, K);
printList(head);
deleteList(&head);
return 0;
}
void
printList(ListNode *head)
{
while (head != NULL) {
printf("%d ", head->item);
head = head->next;
}
printf("\n");
}
void
deleteList(ListNode **ptrHead)
{
ListNode *cur = *ptrHead;
ListNode *temp;
while (cur != NULL) {
temp = cur->next;
free(cur);
cur = temp;
}
*ptrHead = NULL;
}
struct _listNode *
reverse(struct _listNode *head, int K)
{
if (! head)
return NULL;
struct _listNode *cur = head;
struct _listNode *next = NULL;
struct _listNode *prev = NULL;
int count = 0;
while (cur != NULL && count < K) {
next = cur->next;
cur->next = prev;
prev = cur;
cur = next;
count++;
}
if (next != NULL)
head->next = reverse(next, K);
return prev;
}
void
reverseKNodes(ListNode **head, int K)
{
*head = reverse(*head, K);
}
Here is the program output:
3 2 1 6 5 4 9 8 7 10

Related

Can you please help me resolve this ReverseRange Function?

I was trying to write a function ReverseRange(struct Node **head, int x, int y) which will reverse a linked list in given range of indices int x and int y. I used a previously defined function Reverse() in one of condition in ReverseRange() but it is not reversing the given list, only printing only one Node with data 'Q'. I don't know if the error is in Print() or Reverse() or ReverseRange() or elsewhere. Please help, Thank you.
#include <stdio.h>
#include <stdlib.h>
struct Node {
char data;
struct Node *next;
};
//insert data in the node
void Insert(struct Node **Head, char data) {
struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
temp->data = data;
temp->next = *Head;
*Head = temp;
}
//find length of linked list
int LengthRec(struct Node *head) {
if (head == NULL)
return 0;
return 1 + LengthRec(head->next);
}
//Reverse a linked list when head is given;
void Reverse(struct Node **head) {
struct Node *prev = NULL;
struct Node *curr = *head;
struct Node *next = NULL;
while (curr != NULL) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
*head = prev;
}
//Reverse list in range x to y;
int ReverseRange(struct Node **H, int x, int y) {
struct Node *Head = *H;
if (Head == NULL)
return -1;
else if (Head->next == NULL)
return -1;
else if (x == y)
return -1;
else if (x > y)
return -1;
else if (LengthRec(Head) >= y) {
if (x == 1 && y == LengthRec(Head)) {
Reverse(&Head);
return 1;
}
/* NOTE::
Code is incomplete, because I found error before
the entire code is written,
*/
}
}
void Print(struct Node **H) {
struct Node *head = *H;
if (head == NULL) {
printf("Head=NULL");
return;
}
printf("\n %c", head->data);
while (head->next != NULL) {
head = head->next;
printf("\t%c", head->data);
}
}
int main() {
struct Node *Head = NULL;
Insert(&Head, 'Q');
Insert(&Head, 'W');
Insert(&Head, 'E');
Insert(&Head, 'R');
Insert(&Head, 'T');
Print(&Head);
Reverse(&Head);
Print(&Head);
ReverseRange(&Head, 1, 5);
Print(&Head);
}
Output:
T R E W Q
Q W E R T
Q
The Reverse function seems fine, but it should be called with H as an argument from ReverseRange(), not &Head which is a local variable.
Some of the explicit tests at the beginning of the function correspond to legitimate arguments, and should not return an error value.
Note also that you should document the precise semantics for x and y: it is not idiomatic in C to use 1 to designate the first element of a collection, 0 is mote common. y seems included, which is not idiomatic either but consistent with using 1 for the first element.
Your LengthRec function is very inefficient and may cause a stack overflow for very long lists. Use a loop instead of recursing as this recursion is not a tail recursion.
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
struct Node {
char data;
struct Node *next;
};
//insert data in the node
void Insert(struct Node **Head, char data) {
struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
temp->data = data;
temp->next = *Head;
*Head = temp;
}
//find length of linked list
int ListLength(const struct Node *head) {
int length = 0;
while (head != NULL) {
length++;
head = head->next;
}
return length;
}
//Reverse a linked list when head is given;
void Reverse(struct Node **head) {
struct Node *prev = NULL;
struct Node *curr = *head;
while (curr != NULL) {
struct Node *next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
*head = prev;
}
//Reverse list in range x to y;
int ReverseRange(struct Node **H, int x, int y) {
int length = ListLength(*H);
if (x < 1 || x > length || x > y || y > length)
return -1;
if (x == y)
return 1;
if (x == 1 && y == length) {
Reverse(H);
return 1;
} else {
struct Node **head = H;
struct Node *prev = NULL;
struct Node *curr = *head;
struct Node *last;
while (x > 1) {
head = &curr->next;
curr = *head;
x--;
y--;
}
last = curr;
while (x <= y) {
struct Node *next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
x++;
}
last->next = curr;
*head = prev;
return 1;
}
}
void Print(const char *msg, const struct Node *head) {
if (msg) {
printf("%s", msg);
}
if (head == NULL) {
printf("Head=NULL\n");
return;
}
printf("%c", head->data);
while (head->next != NULL) {
head = head->next;
printf("\t%c", head->data);
}
printf("\n");
}
int main() {
struct Node *Head = NULL;
Insert(&Head, 'E');
Insert(&Head, 'D');
Insert(&Head, 'C');
Insert(&Head, 'B');
Insert(&Head, 'A');
Print(" Initial list:\t", Head);
Reverse(&Head);
Print(" Reverse(&Head):\t", Head);
ReverseRange(&Head, 1, 5);
Print("ReverseRange(&Head,1,5):\t", Head);
ReverseRange(&Head, 1, 1);
Print("ReverseRange(&Head,1,1):\t", Head);
ReverseRange(&Head, 2, 4);
Print("ReverseRange(&Head,2,4):\t", Head);
return 0;
}
Output:
Initial list: A B C D E
Reverse(&Head): E D C B A ReverseRange(&Head,1,5): A B C D E
ReverseRange(&Head,1,1): A B C D E
ReverseRange(&Head,2,4): A D C B E

Why am I getting a segmentation fault on my bubble sort?

The bubble sort in my code works, but when the program goes to print the newly sorted list I get a segmentation fault.
I print out the swap sequence and it shows that it is correct. The program segmentation faults after the sorting happens and it goes to print the list in order. I'm not sure exactly whats going wrong here.
#include <stdio.h>
#include <stdlib.h>
typedef struct node_t {
int num;
struct node_t *next;
struct node_t *prev;
} node_t;
void add_node (struct node_t **head, int num) {
struct node_t *new = (struct node_t*)malloc(sizeof(struct node_t));
struct node_t *last = *head;
new->num = num;
new->next = NULL;
if (*head == NULL) {
new->prev = NULL;
*head = new;
return;
} else {
while (last->next != NULL) {
last = last->next;
}
last->next = new;
new->prev = last;
}
return;
}
void swap_num(struct node_t **first, struct node_t **second) {
struct node_t *temp;
printf("%d %d\n", (*first)->num, (*second)->num);
temp->num = (*first)->num;
(*first)->num = (*second)->num;
(*second)->num = temp->num;
}
void sort(struct node_t **head) {
int swapped;
struct node_t *temp;
if (*head == NULL){
printf("list is empty...\n");
return;
}
do {
swapped = 0;
temp = *head;
while (temp->next != NULL) {
if (temp->num > temp->next->num) {
swap_num(&temp, &(temp->next));
swapped = 1;
}
temp = temp->next;
}
} while (swapped);
}
void print_list (struct node_t **head) {
struct node_t *temp;
if (*head != NULL) {
temp = *head;
while (temp != NULL) {
printf("%d ", temp->num);
temp = temp->next;
}
printf("\n");
}
}
int main (void) {
struct node_t *head = NULL;
int new_num, x, y, kill;
while (new_num != 0) {
scanf("%d", &new_num);
if (new_num != 0) {
add_node(&head, new_num);
print_list(&head);
}
}
print_list(&head);
sort(&head);
printf("------------------\n");
print_list(&head);
return 0;
}
This seems to be your problem right here:
..\main.c: In function 'swap_num':
..\main.c:40:15: error: 'temp' is used uninitialized in this function [-Werror=uninitialized]
temp->num = (*first)->num;
~~~~~~~~~~^~~~~~~~~~~~~~~
I got this using compile options such as -Wall, -Wextra, and -Werror. If I fix that, your code does not crash. To fix it I just used an int temporary to hold the value instead of a struct node_t*. Here's my revised swap_num() function:
void swap_num(struct node_t **first, struct node_t **second)
{
int temp;
printf("%d %d\n", (*first)->num, (*second)->num);
temp = (*first)->num;
(*first)->num = (*second)->num;
(*second)->num = temp;
}

Segmentation fault in C while implementing deque

I am trying to implement a deque in C. I am learning C, so the error might seem very trivial. Here's the entire program
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node *prev;
struct node *next;
}Node;
Node *getNode(int data){
Node *temp = (Node *)malloc(sizeof(Node));
temp -> next = temp -> prev = NULL;
temp -> data = data;
return temp;
}
void push_right(Node **head, Node **tail, int data){
Node *newNode = getNode(data);
Node *temp = (*head);
if (temp == NULL){
*head = newNode;
}
else{
while(temp -> next != NULL){
temp = temp -> next;
}
temp -> next = newNode;
newNode -> prev = temp;
*tail = newNode;
}
}
void push_left(Node **head, Node **tail, int data){
Node *newNode = getNode(data);
Node *temp = (*head);
if (temp == NULL){
*head = newNode;
}
else{
newNode -> next = temp;
newNode -> prev = NULL;
(*head) = newNode;
}
}
void remove_right(Node **head, Node **tail, int *val){
Node *temp = (*tail);
if (temp == NULL)
{
printf("Cannot be removed doesn't point to anything\n");
return;
}
else{
*val = temp -> data;
temp = temp -> prev;
(*tail) = temp;
}
free(temp);
}
void remove_left(Node **head, Node **tail, int *val){
Node *temp = (*head);
if (temp == NULL)
{
printf("Cannot be removed doesn't point to anything\n");
return;
}
else{
*val = temp -> data;
temp = temp -> next;
(*tail) = temp;
}
free(temp);
}
void print_all(Node *head){
Node *temp = head;
printf("\n");
while(temp != NULL){
printf("%d\n",temp->data);
temp = temp -> next;
}
}
int main(int argc, char const *argv[])
{
int *val = NULL;
Node *head = NULL;
Node *tail = NULL;
for (int i = 0; i < 10; ++i){
push_right(&head, &tail,i);
push_left(&head, &tail,i);
}
remove_left(&head, &tail, val);
print_all(head);
return 0;
}
The problem seems to arise when remove_left() is called. I have spend a significant amount of time to understand where the problem is coming from but nothing seems to work. Please help.
This is the output on the Terminal screen.
lib2s-iMac:queue admin$ ./a.out
Segmentation fault: 11
Problem is here :
*val = temp -> data;
Val is NULL, so trying to dereference it will result in segmentation fault.
If you change val type to an int instead of a pointer to an int. And then call remove_left like this :
int main(int argc, char const *argv[])
{ int val = 0;
Node *head = NULL;
Node *tail = NULL;
for (int i = 0; i < 10; ++i){
push_right(&head, &tail,i);
push_left(&head, &tail,i);
}
remove_left(&head, &tail, &val);
print_all(head);
return 0;
}
This should work.
In your program have 3 main mistakes .
At int *val = NULL; Here it should be int val not int *val.
At function remove_left() . Here You should edit (*head) not (*tail).Also pointer (next,perv) are not managed properly.
Same in the case of remove_right(). Mistakes in managing pointers.
Modified Functions :-
void remove_right(Node **head, Node **tail, int *val) // modified This function.
{
Node *temp = (*tail);
if (temp == NULL)
{
printf("Cannot be removed doesn't point to anything\n");
return;
}
else
{
*val = temp->data;
temp->prev->next = NULL;
(*tail) = temp->prev;
}
free(temp);
}
void remove_left(Node **head, Node **tail, int *val) // modified This function.
{
Node *temp = (*head);
if (temp == NULL)
{
printf("Cannot be removed doesn't point to anything\n");
return;
}
else
{
*val = temp->data;
temp->next->prev = NULL;
(*head) = temp->next;
}
free(temp);
}
Modified main() :-
int main(int argc, char const *argv[])
{
int val = -1; // Modified here
Node *head = NULL;
Node *tail = NULL;
for (int i = 0; i < 10; ++i)
{
push_right(&head, &tail, i);
push_left(&head, &tail, i);
}
remove_left(&head, &tail, &val);
print_all(head);
return 0;
}
Complete code Online.
Output :-
8
7
6
5
4
3
2
1
0
0
1
2
3
4
5
6
7
8
9

c linked list delete smallest element [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Lists seem really hard for me. I want to find the smallest element in a file: 1 5 8 6 4 8 6 48 9. It's 1 and I want to delete that 1. I can find the smallest element but can not delete it. I find the smallest element place but not the value. I tried copying deleting function from the web, however I cant understand it due to the fact that I'm really new to C. It writes an error that dereferencing to incomplete type. Please help. Post whole code because it should be more convenient to understand.
#include <stdio.h>
#include <stdlib.h>
typedef struct linkedList {
int value;
struct linkedList *next;
} linkedList, head;
linkedList *readList(linkedList *head) {
FILE *dataFile;
dataFile = fopen("duom.txt", "r");
if (dataFile == NULL) {
printf("Nepasisekė atidaryti failo\n");
} else {
printf("Duomenų failą pavyko atidaryti\n");
}
while (!feof (dataFile))
if (head == NULL) {
head = malloc(sizeof(linkedList));
fscanf(dataFile, "%d", &head->value);
head->next = NULL;
} else {
struct linkedList *current = head;
struct linkedList *temp = malloc(sizeof(linkedList));
while (current->next != NULL) {
current = current->next;
}
fscanf(dataFile, "%d", &temp->value);
current->next = temp;
temp->next = NULL;
}
return head;
}
void search(linkedList *head, int *lowest) {
int a[100];
int i = 0;
int minimum;
int b = 0;
linkedList *current = head;
while (current != NULL) {
a[i] = current->value;
current = current->next;
i++;
}
b = i;
i = 0;
minimum = a[0];
while (b > 0) {
if (minimum > a[i]) {
minimum = a[i];
lowest = i;
}
i++;
b--;
}
}
void deleteNode(struct node **head_ref, int key) {
struct node* temp = *head_ref, *prev;
if (temp != NULL && temp->data == key) {
*head_ref = temp->next; // Changed head
free(temp); // free old head
return;
}
while (temp != NULL && temp->data != key) {
prev = temp;
temp = temp->next;
}
if (temp == NULL)
return;
prev->next = temp->next;
free(temp);
}
void printList(linkedList *head) {
linkedList *current = head;
while (current != NULL) {
printf("%d->", current->value);
current = current->next;
}
printf("NULL\n");
return;
}
int main() {
linkedList *A = NULL;
A = readList(A);
search(A);
head = head->next;
minimum = head->value;
headk->next = head->next;
free(head);
printList(A);
return 0;
}
like this
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
typedef struct linkedList{
int value;
struct linkedList *next;
} linkedList, node;
linkedList *readList(void){
FILE *dataFile;
dataFile = fopen("duom.txt", "r");
if(dataFile == NULL) {
perror("file open");
return NULL;
}
int v;
node head = {0, NULL}, *curr = &head;
while (1 == fscanf(dataFile, "%d", &v)){
node *new_node = malloc(sizeof(node));
if(new_node == NULL){
perror("malloc");
break;
}
new_node->value = v;
new_node->next = NULL;
curr = curr->next = new_node;
}
fclose(dataFile);
return head.next;
}
int searchMin(linkedList *head){
if(head == NULL){
fprintf(stderr, "%s: The list MUST NOT be NULL.\n", __func__);
return INT_MIN;
}
int min = head->value;
node *p = head->next;
while(p){
if(p->value < min)
min = p->value;
p = p->next;
}
return min;
}
void deleteNode(node **head_ref, int key){
node *curr = *head_ref, *prev = NULL;
while (curr != NULL && curr->value != key){
prev = curr;
curr = curr->next;
}
if (curr == NULL) return;//not found
if(prev)
prev->next = curr->next;
else
*head_ref = curr->next;
free(curr);
}
void printList(linkedList *head){
node *current = head;
while (current != NULL) {
printf("%d->", current->value);
current = current -> next;
}
puts("NULL");
}
void freeList(linkedList *list){
while(list){
node *temp = list;
list = list->next;
free(temp);
}
}
int main(void){
linkedList *A = readList();
int min = searchMin(A);
printList(A);
deleteNode(&A, min);
printList(A);
freeList(A);
return 0;
}
Please try if this program can help you.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
struct node {
int data;
struct node *next;
};
void push(struct node **head_ref, int new_data) {
struct node *new_node = (struct node *) malloc(sizeof(struct node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void deleteNode(struct node **head_ref, int key) {
struct node *temp = *head_ref, *prev;
if (temp != NULL && temp->data == key) {
*head_ref = temp->next;
free(temp);
return;
}
while (temp != NULL && temp->data != key) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) return;
prev->next = temp->next;
free(temp);
}
void printList(struct node *node) {
while (node != NULL) {
printf(" %d ", node->data);
node = node->next;
}
}
void min(struct node **q) {
struct node *r;
int min = INT_MAX;;
r = *q;
while (r != NULL) {
if (r->data < min) {
min = r->data;
}
r = r->next;
}
printf("The min is %d", min);
deleteNode(q, min);
printf("\n");
}
int main() {
struct node *head = NULL;
FILE *file = fopen("duom.txt", "r");
int i = 0;
fscanf(file, "%d", &i);
while (!feof(file)) {
push(&head, i);
fscanf(file, "%d", &i);
}
fclose(file);
puts("Created Linked List: ");
printList(head);
min(&head);
puts("\nLinked List after Deletion of minimum: ");
printList(head);
return 0;
}
file duom.txt
1 5 8 6 4 8 6 48 9
Test
./a.out
Created Linked List:
9 48 6 8 4 6 8 5 1 The min is 1
Linked List after Deletion of minimum:
9 48 6 8 4 6 8 5 ⏎

C Programming Listnode insert node resulting in infinite loop

I was having some problem when trying to do a double deferencing when inserting node into ListNode. Here is the code:
#include "stdafx.h"
#include <stdlib.h>
typedef struct _listnode {
int num;
struct _listnode *next;
}ListNode;
void insertNode(ListNode **ptrHead, int index, int value);
ListNode *findNode(ListNode *head, int index);
int main()
{
int index, value, i;
ListNode **ptrHead, *head = NULL;
ptrHead = &head;
for (i = 0; i < 5; i++){
printf("Enter value: ");
scanf("%d", &value);
printf("Enter index: ");
scanf("%d", &index);
insertNode(ptrHead, index, value);
}
ptrHead = head;
while (ptrHead != NULL) {
printf("%d", head->num);
ptrHead = head->next;
}
return 0;
}
void insertNode(ListNode **ptrHead, int index, int value) {
ListNode *cur, *newNode;
if (*ptrHead == NULL || index == 0) {
newNode = malloc(sizeof(ListNode));
newNode->num = value;
newNode->next = *ptrHead;
*ptrHead = newNode;
}
else if ((cur = findNode(*ptrHead, index - 1)) != NULL) {
newNode = malloc(sizeof(ListNode));
newNode->num = value;
newNode->next = cur->next;
cur->next = newNode;
}
else printf("Cannot insert the new item at index %d!\n", index);
}
ListNode *findNode(ListNode *head, int index) {
ListNode *cur = head;
if (head == NULL || index < 0)
return NULL;
while (index > 0) {
cur = cur->next;
if (cur == NULL) return NULL;
index--;
}
return cur;
}
So basically I am taking 5 value and index inputs from the user. Then from there, I am inserting them into the ListNode. Inside insertNode(), there is a function called findNode which trying to find the cur so that I can point my cur to the next newNode.
However, with these code, when I try to print out the ListNode, it printed out the first value input infinitely. So I was thinking which part was my mistakes?
Thanks in advance.
In your main function, the following lines of code:
ptrHead = head;
while (ptrHead != NULL) {
printf("%d", head->num);
ptrHead = head->next;
}
shall be:
ListNode *cur = head;
while (cur != NULL) {
printf("%d", cur->num);
cur = cur->next;
}
EDITED:
But can I know why it does not work when I assign head to ptrHead. Is it because I am double deferencing the ptrHead?
They are of different types. ptrHead is ListNode** while head is ListNode*. Thus the assignment ptrHead = head; shall not do what you really want. Also a modern compiler shall have raised some warning on this line.

Resources