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

TWO SUM -LEET CODE PROBLEM 1

   https://leetcode.com/problems/two-sum/ Given an array of integers  nums  and an integer  target , return  indices of the two numbers such that they add up to  target . You may assume that each input would have  exactly  one solution , and you may not use the  same  element twice. You can return the answer in any order.   Example 1: Input: nums = [2,7,11,15], target = 9 Output: [0,1] Output: Because nums[0] + nums[1] == 9, we return [0, 1]. Example 2: Input: nums = [3,2,4], target = 6 Output: [1,2] Example 3: Input: nums = [3,3], target = 6 Output: [0,1] class Solution {     public int[] twoSum(int[] nums, int target) {              HashMap<Integer,Integer> map=new HashMap<Integer,Integer>();                  for(int i=0;i<nums.length;i++){                      ...

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