Merge odd and even positioned nodes of two Linked Lists alternately

Given two linked lists L1 and L2, the task is to print a new list obtained by merging odd position nodes of L1 with the even positioned nodes of L2 alternately.

Examples:

Input: L1 =  8->5->3->2->10->NULL, L2 = 11->13->1->6->9->NULL
Output: 8->13->3->6->10->NULL 
Explanation:
The odd positioned nodes of L1 are {8, 3, 10} and the even positioned nodes L2 are {13, 6}. 
Merging them alternately generates the linked list 8->13->3->6->10->NULL

Input: L1 = 1->5->10->12->13->19->6->NULL, L2 = 2->7->9->NULL  
Output: 1->7->10->13->6->NULL
Explanation:
The odd positioned nodes of L1 are {1, 10, 13, 6} and the even positioned node of L2 is {7}.
Merging them alternately generates the linked list 1->7->10->13->6->NULL

Approach: Follow the steps below to solve the problem:

#Data Structures #Linked List

What is GEEK

Buddha Community

Merge odd and even positioned nodes of two Linked Lists alternately

Merge odd and even positioned nodes of two Linked Lists alternately

Given two linked lists L1 and L2, the task is to print a new list obtained by merging odd position nodes of L1 with the even positioned nodes of L2 alternately.

Examples:

Input: L1 =  8->5->3->2->10->NULL, L2 = 11->13->1->6->9->NULL
Output: 8->13->3->6->10->NULL 
Explanation:
The odd positioned nodes of L1 are {8, 3, 10} and the even positioned nodes L2 are {13, 6}. 
Merging them alternately generates the linked list 8->13->3->6->10->NULL

Input: L1 = 1->5->10->12->13->19->6->NULL, L2 = 2->7->9->NULL  
Output: 1->7->10->13->6->NULL
Explanation:
The odd positioned nodes of L1 are {1, 10, 13, 6} and the even positioned node of L2 is {7}.
Merging them alternately generates the linked list 1->7->10->13->6->NULL

Approach: Follow the steps below to solve the problem:

#Data Structures #Linked List

August  Larson

August Larson

1624320600

How To Create a Linked List in Python

Practice your Python skills by implementing a linked list

What Is a Linked List?

A linked list is a basic data structure. It is a collection of nodes that connects each node to the next node with a pointer.

Each node consists of two items:

  • Data (such as a number)
  • Pointer to the next node

Here is an illustration of a linked list

Diagram explaining linked lists

A linked list forms a chain of nodes. Each node holds data and points to the next node. Image by the author.

#programming #coding #python #how to create a linked list in python #create a linked list #linked list

Rylan  Becker

Rylan Becker

1622127960

Linked List in Data Structure

Linked List in Data Structure

In computer science, we refer to the format of storing data on storage devices as a data structure. An array is the collection of data stored in contiguous memory locations.

This makes accessibility very easy but it leads to not so efficient use of memory because the smaller chunks of memory that are left between earlier stored data are not used.

But what if you need more efficient utilization of your memory. Here dynamic data structures come into the picture. Dynamic data structures as the name suggest do not have a fixed memory size.

They can shrink or grow themselves by deallocating or allocating the memory respectively, as and when required.

In this article, we will learn about the most basic dynamic data structures, Linked list, their types, and how to do basic operations on a linked list.

Linked List in Data Structure

A linked list is the most basic type of dynamic data structure. A linked list is a linear collection of data where the order of elements is not given by their physical memory address. The user stores the data in blocks of memory called nodes.

Each node has two parts. The first part contains the data and the second part points towards the next node. It contains the address of the next node in the actual memory.

Types of Linked Lists

1. Singly Linked list

Singly-linked lists are the most basic and easy to implement linked lists. Each node stores the data at random memory locations and contains a pointer that points to the next node in the memory.

2. Doubly linked list

Each node contains data and two pointers. One point to the previous node and the other points to the next node. This makes doubly-linked traversable both forward and backward.

3. Circular linked list

Just like a singly linked list, here also each node is linked to the next only with a slight modification. In a circular linked list, the last node of the list is pointed to the first node forming a loop. So we can reach the previous node by traversing forward.

In this article, we will be learning about Singly-linked lists only. We will learn about other types in detail in future articles.

Singly Linked list in Data Structure

A singly linked list as we have seen contains multiple nodes in which the earlier node points at the next node except the last node. A node has two parts, the first one contains the data and the second part contains the address of the next node.

Creating Node in Linked List

C program for creating a Linked List Node:

struct node { int data; // data to store in node struct node *next; //NULL for last node }

The previous part of the first node and the next part of the last node will always have NUL

Need of Pointers in Linked List

Unlike arrays, in a linked list, memory allocation takes place at runtime and therefore memory location of each node is random. Each node can be present anywhere in the memory and to access them, each node has the address of its next node stored with it.

This forms a kind of link between every node. This is the additional pointer that we need because if the link is not present or it’s broken, the memory locations will be lost.

Basic Linked List Operations

  • Insert: Insert the node at any position. Worst-case time complexity O(1).
  • Delete: Delete the node at any position. Worst-case time complexity O(1).
  • **Search: **Search for a specific node in the list. The worst-case time complexity O(n).
  • Display: Display a complete list. The worst-case time complexity O(n).

Adding Nodes in Linked List

Adding nodes in a linked list can be sometimes tricky. Since a linked list is a dynamic data structure, we can not just break a link and add or delete the node anywhere. To better understand this let us do a thought experiment.

Consider a linked list similar to a train with multiple coaches. Each coach represents the first part of the node where data is present, here passengers. The next part of the coach, i.e. the coupling represents the second part of the node. This part contains a connection to the next node.

Now, if you want to add a new coach to a moving train you cannot simply uncouple any two coaches and add a new coach. As soon as you uncouple the coaches, the second part of the train which is not attached to the engine will be left behind and that will fail the task.

Similarly, in the linked list, we cannot just break a link between any two nodes and add a new node. Suppose you want to add a new node between the fourth and fifth nodes. To do that you first have to traverse from the first node to the fifth node.

Point the new node to the fifth node and then traverse again from the start to the fourth node. Point the fourth node towards the new node. The new node is now a part of the linked list.

#data structure tutorials #data structures #delete node in linked list #linked list

Hire Dedicated Node.js Developers - Hire Node.js Developers

If you look at the backend technology used by today’s most popular apps there is one thing you would find common among them and that is the use of NodeJS Framework. Yes, the NodeJS framework is that effective and successful.

If you wish to have a strong backend for efficient app performance then have NodeJS at the backend.

WebClues Infotech offers different levels of experienced and expert professionals for your app development needs. So hire a dedicated NodeJS developer from WebClues Infotech with your experience requirement and expertise.

So what are you waiting for? Get your app developed with strong performance parameters from WebClues Infotech

For inquiry click here: https://www.webcluesinfotech.com/hire-nodejs-developer/

Book Free Interview: https://bit.ly/3dDShFg

#hire dedicated node.js developers #hire node.js developers #hire top dedicated node.js developers #hire node.js developers in usa & india #hire node js development company #hire the best node.js developers & programmers

Solving LeetCode’s ‘Swapping Nodes in a Linked List’

In this article, we will be solving LeetCode’s  Swapping Nodes in a Linked List in JavaScript.

Here is an article I wrote on Linked Lists and goes through a LeetCode problem.

Recap, a linked list is a linear collection of nodes where each node points to the next node. Each node has a value property and a next property which references the next node in the list.

This is what makes linked lists a linear data structure. There is a sequence and an order to the list. To get to the end of the list, we must start at the first node (head) and go through all of the nodes in order. The final node (tail) will point to null, indicating that is the end of the list.

Problem

You are given the head of a linked list, and an integer k.

Return the head of the linked list after swapping the values of the kth node from the beginning and the kth node from the end (the list is 1-indexed).

Example

Swapping Nodes

Input: head = [1,2,3,4,5], k = 2

Output: [1,4,3,2,5]

#data-structures #linked-lists #coding #node