Below you will find pages that utilize the taxonomy term “LINKEDLIST”
Posts
Linked List
INFORMATION In computer science, a linked list is a linear collection of data elements whose order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure consisting of a collection of nodes which together represent a sequence.
There are several types of linked lists:
Singly linked list Doubly linked list Circular linked list Circular doubly linked list IMPLEMENTATION C Language .
read morePosts
Reverse Linked List
PROBLEM Given the head of a singly Linked List, reverse the list, and return the reversed list.
Example1 *Input*: head = [1,2,3,4,5] *Output*: [5,4,3,2,1] Example2 *Input*: head = [1,2] *Output*: [2,1] Example3 *Input*: head = [] *Output*: [] SOLVING Steps Create prevNode and nextNode to solve this problem nextNode equal head next N head next equal prevNode H prevNode equal head P head equal nextNode return prevNode Example1 P H N ↓ ↓ ↓ NULL [1 -> 2 -> 3 -> 4 -> 5 -> NULL] head next equal prevNode head equal next P H N ↓ ↓ ↓ [NULL <- 1 2 -> 3 -> 4 -> 5 -> NULL] head next equal prevNode head equal next P H N ↓ ↓ ↓ [NULL <- 1 <- 2 3 -> 4 -> 5 -> NULL] head next equal prevNode head equal next P H N ↓ ↓ ↓ [NULL <- 1 <- 2 <- 3 4 -> 5 -> NULL] head next equal prevNode head equal next P H N ↓ ↓ ↓ [NULL <- 1 <- 2 <- 3 <- 4 5 -> NULL] head next equal prevNode head equal next P H N ↓ ↓ ↓ [NULL <- 1 <- 2 <- 3 <- 4 <- 5] NULL] Return prevNode Code class Solution { public: ListNode *reverseList(ListNode *head) { ListNode *nextNode = nullptr; ListNode *prevNode = nullptr; while (head) { nextNode = head->next; head->next = prevNode; prevNode = head; head = nextNode; } return prevNode; } };
read more