Python Linked List: Set a new value of an item in a singly linked list using index value
Last update on January 04 2021 14:02:00 [UTC/GMT +8 hours]Python Linked List: Access a specific item in a singly linked list using index value
Last update on January 04 2021 14:02:06 [UTC/GMT +8 hours]Linked List | Set 2 [Inserting a node]
We have introduced Linked Lists in the previous post. We also created a simple linked list with 3 nodes and discussed linked list traversal.
All programs discussed in this post consider the following representations of linked list.
// A linked list node
class Node
{
public:
int data;
Node *next;
};
// This code is contributed by rathbhupendra
|
// A linked list node
struct Node
{
int data;
struct Node *next;
};
|
// Linked List Class
class LinkedList
{
Node head; // head of list
/* Node Class */
class Node
{
int data;
Node next;
// Constructor to create a new node
Node[int d] {data = d; next = null; }
}
}
|
# Node class
class Node:
# Function to initialize the node object
def __init__[self, data]:
self.data = data # Assign data
self.next = None # Initialize next as null
# Linked List class
class LinkedList:
# Function to initialize the Linked List object
def __init__[self]:
self.head = None
|
/* Linked list Node*/
public class Node
{
public int data;
public Node next;
public Node[int d] {data = d; next = null; }
}
|
// Linked List Class
var head; // head of list
/* Node Class */
class Node {
// Constructor to create a new node
constructor[d] {
this.data = d;
this.next = null;
}
}
// This code is contributed by todaysgaurav
|
In this post, methods to insert a new node in linked list are discussed. A node can be added in three ways
1] At the front of the linked list
2] After a given node.
3] At the end of the linked list.
Modify contents of Linked List
Given a singly linked list containing n nodes. Modify the value of first half nodes such that 1st node’s new value is equal to the last node’s value minus first node’s current value, 2nd node’s new value is equal to the second last node’s value minus 2nd node’s current value, likewise for first half nodes. If n is odd then the value of the middle node remains unchanged.
[No extra memory to be used].
Examples:
Input : 10 -> 4 -> 5 -> 3 -> 6 Output : 4 -> 1 -> 5 -> 3 -> 6 Input : 2 -> 9 -> 8 -> 12 -> 7 -> 10 Output : -8 -> 2 -> -4 -> 12 -> 7 -> 10Asked in Amazon Interview
Linked Lists in Python: An Introduction
Table of Contents
Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Working With Linked Lists in Python
Linked lists are like a lesser-known cousin of lists. They’re not as popular or as cool, and you might not even remember them from your algorithms class. But in the right context, they can really shine.
In this article, you’ll learn:
- What linked lists are and when you should use them
- How to use collections.deque for all of your linked list needs
- How to implement your own linked lists
- What the other types of linked lists are and what they can be used for
If you’re looking to brush up on your coding skills for a job interview, or if you want to learn more about Python data structures besides the usual dictionaries and lists, then you’ve come to the right place!
You can follow along with the examples in this tutorial by downloading the source code available at the link below:
Get the Source Code: Click here to get the source code you’ll use to learn about linked lists in this tutorial.