0% found this document useful (0 votes)
8 views

Linked List Notes

The document provides an overview of linked lists, a dynamic and non-continuous data structure that allows for efficient insertion and deletion of nodes. It discusses the advantages and disadvantages of linked lists, their time complexity, and various types including singly, doubly, and circular linked lists. Additionally, it includes Java implementation examples and practice questions related to linked list operations.

Uploaded by

sarimayubi1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Linked List Notes

The document provides an overview of linked lists, a dynamic and non-continuous data structure that allows for efficient insertion and deletion of nodes. It discusses the advantages and disadvantages of linked lists, their time complexity, and various types including singly, doubly, and circular linked lists. Additionally, it includes Java implementation examples and practice questions related to linked list operations.

Uploaded by

sarimayubi1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Healthy life requires Healthy Algorithm

SZABIST University

Data Structures & Algorithm

Ali Mobin Memon

What is Linked List?

Linked List is a data structure. A data structure that is non-continuous and dynamic sized and
non-indexed data structure. This means once declared you can keep adding more times as
opposed to static array data structure. If linked list is of n = 10 size initially, then you can still store
more values in it. You can change size of same Linked List object / variable. Since, Linked List is
not indexed so you don’t have to worry about it being continuous streamed data structure such
as array 1.

Linked list works by maintaining pointers as opposed to array which uses indexing. Pointers
points to next node and next node points to next node and so on. Hence, its also important to
know that you cannot directly access any node. You must traverse from start to reach specific
node.

Figure 1: Linked List basic example

As can be witnessed in above fig 1.

• Head node points to 1st node


• If head node points to null then linked list is empty
• 1st node pointed to next node via pointer next
• 2nd node pointed by 1st node
• Each node is data and pointer to next element in Linked List data structures.
• Last node’s next is pointing to null which means ending of Linked List

Advantages:

• Dynamic Sized
o Has no size limit.
o During runtime, its size increases

• Non-Continuous
o Since its dynamic it’s not placed in memory in sequential format
o Half data starts at index 100 of memory
o Rest data starts at index 35350 of memory

1
Linked list is A non-continuous streamed, index less collection of dynamic sized & similar typed data
structure

Page | 1
Healthy life requires Healthy Algorithm
SZABIST University

• Efficient Insertion and Deletion


o Array requires shifting of elements while linked list doesn’t
o It simply adds new node between two nodes

• Memory Efficiency
o Only allocates memory as per size of linked list currently containing values
o No empty nodes to waste memory
Disadvantages:

• Cannot Randomly Access

o You cannot access any element directly. You either start from 1st node or end
node if it’s doubly linked list 0(N)

• Extra Memory Usage

o As it contains pointer to next or previous depending on type of linked list


o It requires more memory to store address of previous or next element

• More Complex Implementation

o Due to managing pointers to next and previous nodes

Time Complexity:

In Linked List adding item at beginning requires only 1 operation, which is as follows:

• Update head node value and make new node as first node.
• Now the first node will point to the rest of Linked List

However, adding item (node) at end of Linked List or at position is always going to big O (n)
operations. The reason is simple traversal to last node requires to travel from beginning to end of
the Linked List and then just update pointer of last node to point to next new node. New node at
end will point to null.

Same case for deletion as adding. But searching node requires traversal of each node until you
reach the specific node containing desired data.

Page | 2
Healthy life requires Healthy Algorithm
SZABIST University

Types of Linked List:

1. Singly Linked List

Each node pointing to next node in forward direction and so on till you reach null pointer
which is end of linked list

2. Doubly Linked List

Each node pointing to next node as well as previous node. This means that now you can
traverse in backward as well as forward direction and so on till you reach null pointer
which is end of linked list.

3. Circular

a. Singly Linked List

Each node pointing to next node in forward direction and so on till you reach end
pointer which is now pointing to first node instead of null. Hence, there is no end of
link list via null. But when last node points to first node you realize you are at end of
link list.

Page | 3
Healthy life requires Healthy Algorithm
SZABIST University

b. Doubly Linked List

Each node pointing to next node in forward direction as well as backward direction
and so on till you reach end pointer which is now pointing to first node and first node
now pointing to last node. So, you identify last node when last node points to first
node. There is no null node now if linked list is filled with at least 2 nodes or more.

In circular linked list if there is one node in linked list then last and first point to same
node. if there are no node then head pointer points to null just like normal non-circular
linked list.

Implementation:

class Node {

int data; // Data part of the node


Node next; // Pointer to the next node
Node previous; // Pointer to the previous node
Node head; // The first node of the linked list

// Constructor to create a new node


Node(int data) {

this.data = data;

this.next = null; // Initialize next to null

this.previous = null; // Initialize next to null

}
}

By using abstract datatype, we create a node class that has next, previous and data fields for each
node. Each node will have these fields no matter what. They may point to null but there will always
be next and data node if its singly and previous also if its doubly linked list.

Page | 4
Healthy life requires Healthy Algorithm
SZABIST University

Here Node is not just class but a type as any class. So, Node next or Node previous code
statement means with one object of node class we are creating more node objects as shown
below.

• Singly Linked List

public static void main(String[] args) {

Node Node1 = new Node(21);


Node1.head = Node1;
Node1.next = new Node (24);
Node1.displayList();

Here,

• Node1 is a first node object. Hence, head is pointed to node1 also.


• You add new node by pointing to next node by telling Node1 that Node1’s next node
is new Node with data 24
• So, Node1.next means what node comes after Node1.

• Doubly Linked List

public static void main(String[] args) {

Node Node1 = new Node(21);


Node1.head = Node1;
Node1.next = new Node (24);
Node1.next.prev = Node1;
Node1.displayList();

Here,

• Node1 is a first node object. Hence, head is pointed to node1 also.


• You add new node by pointing to next node by telling Node1 that Node1’s next
element is new Node with data 24.
• You tell 2nd node previous pointer to point to first node and 2nd node next pointer
to point to null as end of linked list is pointing to null
• First node, previous pointed to null as its start

Page | 5
Healthy life requires Healthy Algorithm
SZABIST University

• Circular Singly Linked List

As discussed before its last node points to first node. The code below is tricky but ill try to
explain

Java Implementation of class Node

public static void main(String[] args) {

Node Node1 = new Node(21);


Node1.head = Node1;
Node1.next = new Node (24);

Node1.next.prev = Node1;
Node1.next.next = Node1;

Node1.displayList();

Here,

• Node1 is a first node object. Hence, head is pointed to node1.

• You add new node by pointing to next node by telling Node1 that Node1’s next

element is new Node with data 24.

• You tell 2nd node to point to previous node which is first node

• You tell 2nd node to point to next node which is first node in case of circular instead

of null, (Node1.next. next = node1), here node1 is first node

Note: if there is 3rd node then 2nd node’s next pointer will be 3rd node and 3rd
node’s next pointer will be first node and so on. Last node will never point to null in
case of circular

• First node, previous pointed to null

Circular doubly in next page →

Page | 6
Healthy life requires Healthy Algorithm
SZABIST University

• Circular Doubly Linked List

public static void main(String[] args) {

Node Node1 = new Node(21);


Node1.head = Node1;
Node1.next = new Node (24);

//here next is new node and new nodes previous pointer is set to first node
Node1.next.prev = Node1;

//here next is new node and new nodes next pointer is set to 1st node
Node1.next.next = Node1;

//here Node1 is first node and its previous pointer is set to last node
Node1.prev= Node1.next;

Node1.displayList();

It is as shown below

Result will be

1. 1st node points to 2nd node and 1st node points to last node. In the above example
2nd node is also last node of the linked list. So, first node previous pointer will also
point to 2nd node aka last node.

2. 2nd node or last node will point to first node and 2nd node previous points to 1st node
also.

Node 2  Node 1 → Node 2 → Node 1


→  

Here,

• Node1 is a first node object. Hence, head is pointed to node1.


• You add new node by pointing to next node by telling Node1 that Node1’s next
element is new Node with data 24.
• You tell 2nd node to point to previous node which is first node
• You tell 2nd node to point to next node which is first node in case of circular instead
of null, (Node1.next. next = node1), here node1 is first node

Page | 7
Healthy life requires Healthy Algorithm
SZABIST University

Note: if there is 3rd node then 2nd node’s next pointer will be 3rd node and 3rd
node’s next pointer will be first node and so on. Last node will never point to null in
case of circular

• First node, previous pointed to null

Practice Questions

1. Add two nodes in between Linked List

2. Merge two linked lists

package com.company;

class Node {
// Data part of the node
Node next; // Pointer to the next node
Node previous; // Pointer to the previous node
Node head; // The first node of the linked list data structure
Node last; // last node of link list
int data;

// Constructor to create a new node


Node(int data) {
this.data = data;
this.next = null; // Initialize next to null
this.previous = null; // Initialize next to null
}

public void displayList() {


Node current = head;

// If the list is empty


if (current == null) {
System.out.println("The list is empty.");
return;
}

// Traverse and print each node's data


while (current != null) {
System.out.print(current.data + " -> ");
current = current.next;
}
// Indicating end of the list
System.out.println("null");

Page | 8
Healthy life requires Healthy Algorithm
SZABIST University

}
}

public class Main {

public static void main(String[] args) {


Node linkList1 = new Node(25);
linkList1.head = linkList1;
linkList1.next = new Node(254);
linkList1.next.next = new Node(125);
linkList1.next.next.next = new Node(113);
linkList1.last = linkList1.next.next.next;

Node linkList2 = new Node(56);


linkList2.head = linkList2;
linkList2.next = new Node(33);
linkList2.next.next = new Node(15);
linkList2.next.next.next = new Node(19);
linkList2.last = linkList2.next.next.next;

System.out.println("First Link List");


linkList1.displayList();

System.out.println("Second Link List");


linkList2.displayList();

// first link list is pointing next to


linkList1.last.next = linkList2;

System.out.println("Merged Link List");


linkList1.displayList();
}

3. Swap 3rd node with 4th node

4. If Next element has same data and is duplicate remove from linked list

Page | 9
Healthy life requires Healthy Algorithm
SZABIST University

5. Reverse the linked list

package com.company;// Iterative Java program to reverse a linked list

class Node {
int data;
Node next;

Node(int new_data) {
data = new_data;
next = null;
}
}

// Given the head of a list, reverse the list and return the
// head of reversed list
public class Main {
static Node reverseList(Node head) {

// Initialize three pointers: curr, prev and next


Node curr = head;
Node prev = null;
Node next;

// Traverse all the nodes of Linked List


while (curr != null) {

// Store next
next = curr.next;

// Reverse current node's next pointer


curr.next = prev;

// Move pointers one position ahead


prev = curr;
curr = next;
}

// Return the head of reversed linked list


return prev;
}

// This function prints the contents


// of the linked list starting from the head
static void printList(Node node) {
while (node != null) {
System.out.print(" " + node.data);
node = node.next;
}
}

Page | 10
Healthy life requires Healthy Algorithm
SZABIST University

public static void main(String[] args) {

// Create a hard-coded linked list:


// 1 -> 2 -> 3 -> 4 -> 5
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);

System.out.print("Given Linked list:");


printList(head);

head = reverseList(head);

System.out.print("\nReversed Linked List:");


printList(head);
}
}

6. Create a function where while calling you just tell which node you want to delete, and it
deletes that node

7. Reverse linked list

package com.company;

import java.util.ArrayList;
import java.util.LinkedList;

class Node {
// Data part of the node
Node next; // Pointer to the next node
Node head; // The first node of the linked list data structure
int data;

// Constructor to create a new node


Node(int data) {
this.data = data;
this.next = null; // Initialize next to null
}
}

public class Main {

public static void displayList(Node head) {


Node current = head;
if (current == null) {
System.out.println("The list is empty.");
return;
}

Page | 11
Healthy life requires Healthy Algorithm
SZABIST University

// Traverse and print each node's data


while (current != null) {
System.out.print(current.data + " -> ");
current = current.next;
}
System.out.println("null"); // Indicating end of the list
}

public static Node reverse(Node head){


Node curr = head;
Node prev = null;
Node next;

while(curr != null){
// we are simply converting next node to prev node creating a reverse cycle
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
public static void main(String[] args) {
Node linkList1 = new Node(25);
linkList1.head = linkList1;
linkList1.next = new Node(254);
linkList1.next.next = new Node(125);
linkList1.next.next.next = new Node(113);

linkList1 = reverse(linkList1);
displayList(linkList1);
}
}

8. Convert linked list into an array

9. Print the middle node of linked list. If its even sized linked list print the right node of the
middle. Such as following

523 → 554 → 102 → 352 → Null


In this there is no middle then print the right node which is 4th node as 554 and 102 are
both in middle.

10. Reverse a doubly linked list

Page | 12

You might also like