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

Nth Node from End in LinkedList

Nth Node from End in LinkedList 1)  Find length of  linked list. 2) Traverse (length-position+1) element from begin. //nth Node from end public Node nodeFromEnd ( Node head , int position ){ Node temp = head ; int length = 1 ; while ( temp . next != null ) { length ++; temp = temp . next ; } System . out . println ( "length" + length ); temp = head ; int c = 1 ; while ( c !=( length - position + 1 )) { temp = temp . next ; c ++; } return temp ; }

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