Linked List Set 1

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Write a function to reverse a linked list (recursively)

1. Divide the list in two parts, first node and rest of the linked list. first = *head_ref; rest = first->next; If list has only one node if (rest == NULL) return; 2. Reverse the rest list and put the first element at the end recursiveReverse(&rest); first->next->next = first; 3. / tricky step / first->next = NULL; / fix the head pointer / *head_ref = rest;

Write a C function to detect loop in a linked list

1. Traverse the list one by one and keep putting the node addresses in a Hash Table. At any point, if NULL is reached then return false and if next of current node points to any of the previously stored nodes in Hash then return true 2. Traverse linked list using two pointers. Move one pointer by one and other pointer by two. If these pointers meet at some node then there is a loop. If pointers do not meet then linked list doesn't have loop. (Floyd's Cycle-Finding Algorithm)

Given a singly linked list of characters, write a function that returns true if the given list is palindrome, else false.

1. Use a stack of list nodes. Traverse the given list from head to tail and push every visited node to stack. Traverse list again. For every visited node, pop a node from stack and compare data of popped node with currently visited node. Time complexity is O(n), but it requires O(n) extra space. 2. Get the middle of the linked list. Reverse the second half of the linked list. Check if the first half and second half are identical.

Write a function to delete a Linked List

Iterate through the linked list and delete all the nodes one by one. Main point here is not to access next of the current pointer if current pointer is deleted.

Write a function to reverse a linked list (iteratively)

Iterate trough the linked list. In loop, change next to prev, prev to current and current to next.

You are given a Double Link List with one pointer of each node pointing to the next node just like in a single link list. The second pointer however CAN point to any node in the list and not just the previous node. WAP to duplicate this list in O(n) time.

Method 1 (Uses O(n) extra space): Stores the next and arbitrary mappings (of original list) in an array first, then modifies the original Linked List (to create copy), creates a copy. And finally restores the original list.

Given only a pointer to a node to be deleted in a singly linked list, how do you delete it?

copy the data from the next node to the node to be deleted and delete the next node.


Kaugnay na mga set ng pag-aaral

Begin: Exam - Chapters 12 and 13

View Set

Prep-U Chapter 14: Shock and Multiple Organ Dysfunction Syndrome

View Set

Genetics Chapter 19: Gene Mutation, DNA Repair and Recombination

View Set

Chapter 11 Skull & Cranial Bones Workbook

View Set

Cisco CyberOps Associate CBROPS 200-201 Official Cert Guide - Chapter 1

View Set