Skip to main content

How to design a LinkedList ?

To design a Linked List we normally need to design three classes one class for Node that contains a data and link to the next node.

Second class contain a big picture as a combination of nodes where one can view the number of nodes  as well as design functions for it.

Third class contains operations or the methods that we want to perform on the linked lists.This class contains main method and is driving whole mechanism.



Basic Structure of Node:-

Data represent the value and next represent the link to other nodes.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.problems.list;

public class Node {
 int data;
 Node next;
 


 public Node()
 {
  data=0;
  next=null;
 }
 
 public Node(int data)
 {
  this.data=data;
  next=null;
 }
 
 public Node(int data,Node node)
 {
  this.data=data;
  this.next=node;
 }
 
 public int getData() {
  return data;
 }

 public void setData(int data) {
  this.data = data;
 }

 public Node getNext() {
  return next;
 }

 public void setNext(Node next) {
  this.next = next;
 }

 @Override
 public String toString() {
  return "Node [data=" + data + ", next=" + next + "]";
 }
 
}





Basic Structure of Linked List:-

Length represent the current size of list and head represent the first node.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.problems.list;

public class LinkedList {

 public LinkedList() {
  length = 0;
 }
 
 Node head;
 int length;
 public void addNodeStart(LinkedList list,int data)
 {
  Node temp=new Node(data);
  if(list.head==null)
  {
   System.out.println("head is null");
   head=temp;
   length++;
  }
  else{
  
  temp.next=list.head;
  list.head=temp;
  
  }
 }
 
 public void printList(Node head)
 {
  Node temp =head;
  System.out.println("List Data");
  while(temp!=null)
  {
   System.out.print(+temp.data+" ");
   temp=temp.next;
  }
  
 }
}
Basic Functions:-

Here we are creating one list then passing the head(first node) to function for creating the node.In second method we are printing the list.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
package com.problems.list;

public class LinkedListOperations {

 public static void main(String[] args)
 {
  LinkedList list=new LinkedList();
  list.addNodeStart(list, 5);
  list.addNodeStart(list, 15);
  list.addNodeStart(list, 25);
  list.printList(list.head);
 }
}

Comments

.

Popular posts from this blog

Calculating size of User and Cache storage

user storage:- it can be solved using same scenario as mentioned in first article . https://tech.nazarmubeenworks.com/2019/08/basics-of-system-design-chapter-0.html Suppose there are 12 Million of users are adding every year meaning 1 Million per month. So if we consider 5 year there will be around 60 million of users. Now in terms of data we can look to our DB table and get information about it. A user will be generally having name , id , address , some forien keys . Lets assume 10 columns with each column on an average storing 4 byte of data. 10*4 = 40 bytes of data for one user. 60 * 10^6 * 40 = 2400 * 10^6 = 2.4 * 10^9 = 2.4 GB of data we need to store only user values. Also while calculating storage for the user there is also one important point we need to remember is of ids. If we are going to have 60 Million of users which means 60*10^6 users so unique id’s will be as we know below figures are almost equal

Best LeetCode Lists for Interviews

Here is a list of some of the best questions asked in interviews:-  Must do 75 https://leetcode.com/list/5hkn6wze/ Must do 60  https://leetcode.com/list/5eie1aqd/ Must do medium:-  https://leetcode.com/list/5xaelz7g/ Must do Easy:-   https://leetcode.com/list/5r7rxpr1/ Graph:-  https://leetcode.com/list/x18ervrd/  Dynamic Programming:-    https://leetcode.com/list/x14z0dxr/  FaceBook interviews:- https://leetcode.com/list/xyu98pv6/  Amazon Interviews:-  https://leetcode.com/list/5hkniyf7/  Google Interviews:- https://leetcode.com/list/xyu9xfo1/ https://github.com/nazarmubeen/TopProblems/blob/master/README.md