Basic operations on Singly linked list

Basic operations on Singly linked list


package Linked_List;


class Node
{
	int data;
	Node next;
	
	public Node(int a)
	{
		this.data = a;
		this.next = null;
	}
	public Node()
	{}
}


public class LinkedList
{
	Node head;
	int size=0;
	
	public void push(int a)
	{
		Node node = new Node(a);
		
		if(head == null)
			head = node;
		else
		{
			Node temp = head;
			for(temp=head; temp.next != null;  temp = temp.next)
			{}
			temp.next = node;
		}
		size++;
	}
	
	public int size()
	{
		Node temp = this.head.next;
		int i = 1;
		while(temp != head   &&   temp != null)
		{
			temp = temp.next;
			i++;
		}
		return i;
	}
}

Comments

Popular posts from this blog

4.Sort an array of 0's, 1's & 2's.

1. Reverse

3. Height of Binary tree