Each node in a list consists of at least two parts:
1. Data
2. Pointer to the next node
In C/C++, we can represent a node of Linked List using structures or classes.
In Java and Python, Linked List can be represented as a class and a Node as a separate class. The LinkedList class contains a reference of Node class type.
// A linked list node
struct ListNode
{
int val;
struct ListNode *next;
};
// Linked list class
class ListNode
{
// head of list
Node head;
// Node class
class Node
{
int val;
Node next;
// Constructor to create a new node
Node(int v) {
val = v;
}
}
}
# Node class
class Node:
# Function to initialize the node object
def __init__(self, v):
self.val = v # Assign value
self.next = None # Initialize next as null
# Linked List class
class ListNode:
# Function to initialize the Linked List
def __init__(self):
self.head = None
Walkthrough Example :
| Problem | Score | Companies | Time | Status |
|---|---|---|---|---|
| Sort Binary Linked List | 200 |
|
33:21 | |
| Partition List | 275 |
|
49:36 | |
| Insertion Sort List | 300 |
|
49:25 | |
| Sort List | 350 |
|
59:53 |
| Problem | Score | Companies | Time | Status |
|---|---|---|---|---|
| Palindrome List | 200 |
|
47:06 | |
| Remove Duplicates from Sorted List II | 300 |
|
57:51 | |
| Merge Two Sorted Lists | 300 |
|
29:11 | |
| Remove Duplicates from Sorted List | 300 |
|
17:58 | |
| Remove Nth Node from List End | 350 |
|
27:38 |
| Problem | Score | Companies | Time | Status |
|---|---|---|---|---|
| K reverse linked list | 200 |
|
60:30 | |
| Even Reverse | 200 |
|
46:24 | |
| Swap List Nodes in pairs | 350 |
|
33:34 | |
| Rotate List | 350 |
|
33:58 |
| Problem | Score | Companies | Time | Status |
|---|---|---|---|---|
| Kth Node From Middle | 200 |
|
30:13 | |
| Reverse Alternate K Nodes | 300 |
|
54:47 | |
| Reverse Link List II | 450 |
|
57:24 | |
| Reorder List | 600 |
|
57:10 |
| Problem | Score | Companies | Time | Status |
|---|---|---|---|---|
| Add Two Numbers as Lists | 250 |
|
43:07 | |
| List Cycle | 600 |
|
39:15 |