Classes & Objects Revision
What are Linked Lists ?
Basic Functionality
class Node {
int x = 0;
int y = 0;
}
class Node:
x = 0
y = 0
class Node {
int x = 0;
int y = 0;
}
class Main {
public static void main(String[] args) {
Node n1 = new Node();
}
}
class Node:
x = 0
y = 0
def main():
n1 = Node()
main()
class Node {
int x = 0;
int y = 0;
}
class Main {
public static void main(String[] args) {
Node n1 = new Node();
System.out.println(n1.x); // 0
System.out.println(n1.y); // 0
}
}
class Node:
x = 0
y = 0
def main():
n1 = Node()
print(n1.x) # 0
print(n1.x) # 0
main()
class Node {
int x = 0;
int y = 0;
}
class Main {
public static void main(String[] args) {
Node n1 = new Node();
n1.x = 50;
n1.y = 20;
System.out.println(n1.x); // 50
System.out.println(n1.y); // 20
}
}
class Node:
x = 0
y = 0
def main():
n1 = Node()
n1.x = 50
n1.y = 20
print(n1.x) # 50
print(n1.y) # 20
main()
class Node {
int x = 0;
int y = 0;
}
class Main {
public static void main(String[] args) {
Node n1 = new Node();
n1.x = 50;
n1.y = 20;
Node n2 = n1;
System.out.println(n1.x);
System.out.println(n1.y);
}
}
class Node:
x = 0
y = 0
def main():
n1 = Node()
n1.x = 50
n1.y = 20
n2 = n1
print(n1.x)
print(n1.y)
main()
class Node {
int x = 0;
int y = 0;
}
class Main {
public static void main(String[] args) {
Node n1 = new Node();
n1.x = 50;
n1.y = 20;
Node n2 = n1;
n2.x = 100;
System.out.println(n1.x);
System.out.println(n1.y);
}
}
class Node:
x = 0
y = 0
def main():
n1 = Node()
n1.x = 50
n1.y = 20
n2 = n1
n2.x = 100
print(n1.x)
print(n1.y)
main()
class Node {
int x = 0;
int y = 0;
}
class Main {
public static void main(String[] args) {
Node n1 = new Node();
n1.x = 50;
n1.y = 20;
Node n2 = n1;
n2.x = 100;
Node n3 = n1;
System.out.println(n3.x); // 100
System.out.println(n3.y); // 20
}
}
class Node:
x = 0
y = 0
def main():
n1 = Node()
n1.x = 50
n1.y = 20
n2 = n1
n2.x = 100
n3 = n1
print(n3.x) # 100
print(n3.y) # 20
main()
class Node {
int x = 0;
int y = 0;
}
class Main {
public static void main(String[] args) {
Node n1 = new Node();
n1.x = 50;
n1.y = 20;
Node n2 = n1;
n2.x = 100;
Node n3 = n1;
System.out.println(n3.x); // 100
System.out.println(n3.y); // 20
Node n4 = new Node();
}
}
class Node:
x = 0
y = 0
def main():
n1 = Node()
n1.x = 50
n1.y = 20
n2 = n1
n2.x = 100
n3 = n1
print(n3.x) # 100
print(n3.y) # 20
n4 = Node()
main()
class Node {
int x = 0;
int y = 0;
}
class Main {
public static void main(String[] args) {
Node n1 = new Node();
n1.x = 50;
n1.y = 20;
Node n2 = new Node();
n2.x = -20;
n2.y = 10;
}
}
class Node:
x = 0
y = 0
def main():
n1 = Node()
n1.x = 50
n1.y = 20
n2 = Node()
n2.x = -20
n2.y = 10
main()
class Node {
int x = 0;
int y = 0;
Node(int a, int b) {
x = a;
y = b;
}
}
class Main {
public static void main(String[] args) {
Node n1 = new Node(50, 20);
Node n2 = new Node(-20, 10);
}
}
class Node:
x = 0
y = 0
def __init__(self, a, b):
self.x = a
self.y = b
def main():
n1 = Node(50, 20)
n2 = Node(-20, 10)
main()
class Node {
int data = 0;
Node next = null;
}
class Node:
data = 0
next = None
class Node {
int data = 0;
Node next = null;
Node(int x) {
data = x;
next = null;
}
}
class Node:
data = 0
next = None
def __init__(self, x):
self.data = x
self.next = None
class Node {
int data = 0;
Node next = null;
Node(int x) {
data = x;
next = null;
}
}
class Main {
public static void main(String[] args) {
Node t1 = new Node(10);
}
}
class Node:
data = 0
next = None
def __init__(self, x):
self.data = x
self.next = None
def main():
t1 = Node(10)
main()
class Node {
int data = 0;
Node next = null;
Node(int x) {
data = x;
next = null;
}
}
class Main {
public static void main(String[] args) {
Node t1 = new Node(10);
}
}
class Node:
data = 0
next = None
def __init__(self, x):
self.data = x
self.next = None
def main():
t1 = Node(10)
main()
data
next
class Node {
int data = 0;
Node next = null;
Node(int x) {
data = x;
next = null;
}
}
class Main {
public static void main(String[] args) {
Node t1 = new Node(10);
}
}
class Node:
data = 0
next = None
def __init__(self, x):
self.data = x
self.next = None
def main():
t1 = Node(10)
main()
data
next
10
null
class Node {
int data = 0;
Node next = null;
Node(int x) {
data = x;
next = null;
}
}
class Main {
public static void main(String[] args) {
Node t1 = new Node(10);
}
}
class Node:
data = 0
next = None
def __init__(self, x):
self.data = x
self.next = None
def main():
t1 = Node(10)
main()
data
next
10
null
t1
class Node {
int data = 0;
Node next = null;
Node(int x) {
data = x;
next = null;
}
}
class Main {
public static void main(String[] args) {
Node t1 = new Node(10);
t1.next = new Node(20);
}
}
class Node:
data = 0
next = None
def __init__(self, x):
self.data = x
self.next = None
def main():
t1 = Node(10)
t1.next = Node(20)
main()
data
next
10
null
t1
class Node {
int data = 0;
Node next = null;
Node(int x) {
data = x;
next = null;
}
}
class Main {
public static void main(String[] args) {
Node t1 = new Node(10);
t1.next = new Node(20);
}
}
class Node:
data = 0
next = None
def __init__(self, x):
self.data = x
self.next = None
def main():
t1 = Node(10)
t1.next = Node(20)
main()
data
next
10
null
t1
data
next
20
null
class Node {
int data = 0;
Node next = null;
Node(int x) {
data = x;
next = null;
}
}
class Main {
public static void main(String[] args) {
Node t1 = new Node(10);
t1.next = new Node(20);
}
}
class Node:
data = 0
next = None
def __init__(self, x):
self.data = x
self.next = None
def main():
t1 = Node(10)
t1.next = Node(20)
main()
data
next
10
t1
data
next
20
null
class Node {
int data = 0;
Node next = null;
Node(int x) {
data = x;
next = null;
}
}
class Main {
public static void main(String[] args) {
Node t1 = new Node(10);
t1.next = new Node(20);
Node t2 = t1.next;
}
}
class Node:
data = 0
next = None
def __init__(self, x):
self.data = x
self.next = None
def main():
t1 = Node(10)
t1.next = Node(20)
t2 = t1.next
main()
data
next
10
t1
data
next
20
null
class Node {
int data = 0;
Node next = null;
Node(int x) {
data = x;
next = null;
}
}
class Main {
public static void main(String[] args) {
Node t1 = new Node(10);
t1.next = new Node(20);
Node t2 = t1.next;
}
}
class Node:
data = 0
next = None
def __init__(self, x):
self.data = x
self.next = None
def main():
t1 = Node(10)
t1.next = Node(20)
t2 = t1.next
main()
data
next
10
t1
data
next
20
null
t2
class Node {
int data = 0;
Node next = null;
Node(int x) {
data = x;
next = null;
}
}
class Main {
public static void main(String[] args) {
Node t1 = new Node(10);
t1.next = new Node(20);
Node t2 = t1;
t2.next = new Node(30);
}
}
class Node:
data = 0
next = None
def __init__(self, x):
self.data = x
self.next = None
def main():
t1 = Node(10)
t1.next = Node(20)
t2 = t1.next
t2.next = Node(30)
main()
data
next
10
t1
data
next
20
null
t2
data
next
30
class Node {
int data = 0;
Node next = null;
Node(int x) {
data = x;
next = null;
}
}
class Main {
public static void main(String[] args) {
Node t1 = new Node(10);
t1.next = new Node(20);
Node t2 = t1;
t2.next = new Node(30);
t2 = t2.next;
System.out.println(t2.data);
}
}
class Node:
data = 0
next = None
def __init__(self, x):
self.data = x
self.next = None
def main():
t1 = Node(10)
t1.next = Node(20)
t2 = t1.next
t2.next = Node(30)
t2 = t2.next
print(t2.data)
main()
data
next
10
t1
data
next
20
null
t2
data
next
30
class Node {
int data = 0;
Node next = null;
Node(int x) {
data = x;
next = null;
}
}
class Main {
public static void main(String[] args) {
Node t1 = new Node(10);
t1.next = new Node(20);
Node t2 = t1;
t2.next = new Node(30);
t2 = t2.next;
System.out.println(t2.data); // 30
}
}
class Node:
data = 0
next = None
def __init__(self, x):
self.data = x
self.next = None
def main():
t1 = Node(10)
t1.next = Node(20)
t2 = t1.next
t2.next = Node(30)
t2 = t2.next
print(t2.data) # 30
main()
data
next
10
t1
data
next
20
null
t2
data
next
30
data
next
10
t1
data
next
20
null
t2
data
next
30
data
next
10
t1
data
next
20
null
t2
data
next
30
We only need head Node address to access entire chain of data.
data
next
10
t1
data
next
20
null
t2
data
next
30
This type of data structure is called a
data
next
10
t1
data
next
20
null
t2
data
next
30
This type of data structure is called a
Linked List
data
next
10
t1
data
next
20
null
data
next
30
Linked List
A Linked List is a linear data structure consisting of connected nodes where each node has corresponding data and a pointer to the address of the next node.
The first node of a linked list is called the Head, and it acts as an access point.
Given N > 0, create a linked list which contains data from 1 to N.
Node generateLL(int n) {
Node head = new Node(1);
Node temp = head;
for (int i = 2; i <= n; i++) {
temp.next = new Node(i);
temp = temp.next;
}
return head;
}
def generateLL(n):
head = Node(1)
temp = head
for i in range(2, n + 1):
temp.next = Node(i)
temp = temp.next
return head
Given N > 0, create a linked list which contains data from 1 to N.
Node generateLL(int n) {
Node head = new Node(1);
Node temp = head;
for (int i = 2; i <= n; i++) {
temp.next = new Node(i);
temp = temp.next;
}
return head;
}
def generateLL(n):
head = Node(1)
temp = head
for i in range(2, n + 1):
temp.next = Node(i)
temp = temp.next
return head
n = 4
Given N > 0, create a linked list which contains data from 1 to N.
Node generateLL(int n) {
Node head = new Node(1);
Node temp = head;
for (int i = 2; i <= n; i++) {
temp.next = new Node(i);
temp = temp.next;
}
return head;
}
def generateLL(n):
head = Node(1)
temp = head
for i in range(2, n + 1):
temp.next = Node(i)
temp = temp.next
return head
n = 4
data
next
1
head
null
Given N > 0, create a linked list which contains data from 1 to N.
Node generateLL(int n) {
Node head = new Node(1);
Node temp = head;
for (int i = 2; i <= n; i++) {
temp.next = new Node(i);
temp = temp.next;
}
return head;
}
def generateLL(n):
head = Node(1)
temp = head
for i in range(2, n + 1):
temp.next = Node(i)
temp = temp.next
return head
n = 4
data
next
1
head
temp
null
Given N > 0, create a linked list which contains data from 1 to N.
Node generateLL(int n) {
Node head = new Node(1);
Node temp = head;
for (int i = 2; i <= n; i++) {
temp.next = new Node(i);
temp = temp.next;
}
return head;
}
def generateLL(n):
head = Node(1)
temp = head
for i in range(2, n + 1):
temp.next = Node(i)
temp = temp.next
return head
n = 4
data
next
1
head
temp
null
i = 2
Given N > 0, create a linked list which contains data from 1 to N.
Node generateLL(int n) {
Node head = new Node(1);
Node temp = head;
for (int i = 2; i <= n; i++) {
temp.next = new Node(i);
temp = temp.next;
}
return head;
}
def generateLL(n):
head = Node(1)
temp = head
for i in range(2, n + 1):
temp.next = Node(i)
temp = temp.next
return head
n = 4
data
next
1
head
temp
null
i = 2
data
next
2
Given N > 0, create a linked list which contains data from 1 to N.
Node generateLL(int n) {
Node head = new Node(1);
Node temp = head;
for (int i = 2; i <= n; i++) {
temp.next = new Node(i);
temp = temp.next;
}
return head;
}
def generateLL(n):
head = Node(1)
temp = head
for i in range(2, n + 1):
temp.next = Node(i)
temp = temp.next
return head
n = 4
data
next
1
head
temp
null
i = 2
data
next
2
Given N > 0, create a linked list which contains data from 1 to N.
Node generateLL(int n) {
Node head = new Node(1);
Node temp = head;
for (int i = 2; i <= n; i++) {
temp.next = new Node(i);
temp = temp.next;
}
return head;
}
def generateLL(n):
head = Node(1)
temp = head
for i in range(2, n + 1):
temp.next = Node(i)
temp = temp.next
return head
n = 4
data
next
1
head
temp
null
i = 3
data
next
2
Given N > 0, create a linked list which contains data from 1 to N.
Node generateLL(int n) {
Node head = new Node(1);
Node temp = head;
for (int i = 2; i <= n; i++) {
temp.next = new Node(i);
temp = temp.next;
}
return head;
}
def generateLL(n):
head = Node(1)
temp = head
for i in range(2, n + 1):
temp.next = Node(i)
temp = temp.next
return head
n = 4
data
next
1
head
temp
null
i = 3
data
next
2
data
next
3
Given N > 0, create a linked list which contains data from 1 to N.
Node generateLL(int n) {
Node head = new Node(1);
Node temp = head;
for (int i = 2; i <= n; i++) {
temp.next = new Node(i);
temp = temp.next;
}
return head;
}
def generateLL(n):
head = Node(1)
temp = head
for i in range(2, n + 1):
temp.next = Node(i)
temp = temp.next
return head
n = 4
data
next
1
head
temp
null
i = 3
data
next
2
data
next
3
Given N > 0, create a linked list which contains data from 1 to N.
Node generateLL(int n) {
Node head = new Node(1);
Node temp = head;
for (int i = 2; i <= n; i++) {
temp.next = new Node(i);
temp = temp.next;
}
return head;
}
def generateLL(n):
head = Node(1)
temp = head
for i in range(2, n + 1):
temp.next = Node(i)
temp = temp.next
return head
n = 4
data
next
1
head
temp
null
i = 4
data
next
2
data
next
3
Given N > 0, create a linked list which contains data from 1 to N.
Node generateLL(int n) {
Node head = new Node(1);
Node temp = head;
for (int i = 2; i <= n; i++) {
temp.next = new Node(i);
temp = temp.next;
}
return head;
}
def generateLL(n):
head = Node(1)
temp = head
for i in range(2, n + 1):
temp.next = Node(i)
temp = temp.next
return head
n = 4
data
next
1
head
temp
null
i = 4
data
next
2
data
next
3
data
next
4
Given N > 0, create a linked list which contains data from 1 to N.
Node generateLL(int n) {
Node head = new Node(1);
Node temp = head;
for (int i = 2; i <= n; i++) {
temp.next = new Node(i);
temp = temp.next;
}
return head;
}
def generateLL(n):
head = Node(1)
temp = head
for i in range(2, n + 1):
temp.next = Node(i)
temp = temp.next
return head
n = 4
data
next
1
head
temp
null
i = 4
data
next
2
data
next
3
data
next
4
Given N > 0, create a linked list which contains data from 1 to N.
Node generateLL(int n) {
Node head = new Node(1);
Node temp = head;
for (int i = 2; i <= n; i++) {
temp.next = new Node(i);
temp = temp.next;
}
return head;
}
def generateLL(n):
head = Node(1)
temp = head
for i in range(2, n + 1):
temp.next = Node(i)
temp = temp.next
return head
n = 4
data
next
1
head
temp
null
return head
data
next
2
data
next
3
data
next
4
Given the head of a linked list, return its size
Given the head of a linked list, return its size
int size(Node head) {
Node temp = head;
int count = 0;
while (temp != null) {
count++;
temp = temp.next;
}
return count;
}
def size(head):
temp = head
count = 0
while temp != None:
count += 1
temp = temp.next
return count
Given the head of a linked list, return its size
int size(Node head) {
Node temp = head;
int count = 0;
while (temp != null) {
count++;
temp = temp.next;
}
return count;
}
def size(head):
temp = head
count = 0
while temp != None:
count += 1
temp = temp.next
return count
data
next
1
head
null
data
next
2
data
next
3
data
next
4
Given the head of a linked list, return its size
int size(Node head) {
Node temp = head;
int count = 0;
while (temp != null) {
count++;
temp = temp.next;
}
return count;
}
def size(head):
temp = head
count = 0
while temp != None:
count += 1
temp = temp.next
return count
data
next
1
head
temp
null
data
next
2
data
next
3
data
next
4
Given the head of a linked list, return its size
int size(Node head) {
Node temp = head;
int count = 0;
while (temp != null) {
count++;
temp = temp.next;
}
return count;
}
def size(head):
temp = head
count = 0
while temp != None:
count += 1
temp = temp.next
return count
data
next
1
head
temp
null
data
next
2
data
next
3
data
next
4
count = 0
Given the head of a linked list, return its size
int size(Node head) {
Node temp = head;
int count = 0;
while (temp != null) {
count++;
temp = temp.next;
}
return count;
}
def size(head):
temp = head
count = 0
while temp != None:
count += 1
temp = temp.next
return count
data
next
1
head
temp
null
data
next
2
data
next
3
data
next
4
count = 1
Given the head of a linked list, return its size
int size(Node head) {
Node temp = head;
int count = 0;
while (temp != null) {
count++;
temp = temp.next;
}
return count;
}
def size(head):
temp = head
count = 0
while temp != None:
count += 1
temp = temp.next
return count
data
next
1
head
temp
null
data
next
2
data
next
3
data
next
4
count = 1
Given the head of a linked list, return its size
int size(Node head) {
Node temp = head;
int count = 0;
while (temp != null) {
count++;
temp = temp.next;
}
return count;
}
def size(head):
temp = head
count = 0
while temp != None:
count += 1
temp = temp.next
return count
data
next
1
head
temp
null
data
next
2
data
next
3
data
next
4
count = 2
Given the head of a linked list, return its size
int size(Node head) {
Node temp = head;
int count = 0;
while (temp != null) {
count++;
temp = temp.next;
}
return count;
}
def size(head):
temp = head
count = 0
while temp != None:
count += 1
temp = temp.next
return count
data
next
1
head
temp
null
data
next
2
data
next
3
data
next
4
count = 2
Given the head of a linked list, return its size
int size(Node head) {
Node temp = head;
int count = 0;
while (temp != null) {
count++;
temp = temp.next;
}
return count;
}
def size(head):
temp = head
count = 0
while temp != None:
count += 1
temp = temp.next
return count
data
next
1
head
temp
null
data
next
2
data
next
3
data
next
4
count = 3
Given the head of a linked list, return its size
int size(Node head) {
Node temp = head;
int count = 0;
while (temp != null) {
count++;
temp = temp.next;
}
return count;
}
def size(head):
temp = head
count = 0
while temp != None:
count += 1
temp = temp.next
return count
data
next
1
head
temp
null
data
next
2
data
next
3
data
next
4
count = 4
Given the head of a linked list, return its size
int size(Node head) {
Node temp = head;
int count = 0;
while (temp != null) {
count++;
temp = temp.next;
}
return count;
}
def size(head):
temp = head
count = 0
while temp != None:
count += 1
temp = temp.next
return count
data
next
1
head
temp
null
data
next
2
data
next
3
data
next
4
count = 4
Given the head of a linked list, return its size
int size(Node head) {
Node temp = head;
int count = 0;
while (temp != null) {
count++;
temp = temp.next;
}
return count;
}
def size(head):
temp = head
count = 0
while temp != None:
count += 1
temp = temp.next
return count
data
next
1
head
temp
null
data
next
2
data
next
3
data
next
4
return count
count = 4
Given the head node of a linked list, insert a new node at kth index (0 based indexing)
Given the head node of a linked list, insert a new node at kth index (0 based indexing)
Node insert(Node head, int pos, int data) {
if (pos < 0 || pos > size(head)) {
return head;
}
Node temp = head;
Node newNode = new Node(data);
if (pos == 0) {
newNode.next = head;
return newNode;
}
for (int i = 0; i < pos - 1; i++) {
temp = temp.next;
}
newNode.next = temp.next;
temp.next = newNode;
return head;
}
def insert(head, pos, data):
if pos < 0 or pos > size(head):
return head
temp = head
newNode = Node(data)
if pos == 0:
newNode.next = head
return newNode
for i in range(pos - 1):
temp = temp.next
newNode.next = temp.next
temp.next = newNode
return head
Java -
https://ide.codingminutes.com/?id=aii
Python -
https://ide.codingminutes.com/?id=aik