[go: up one dir, main page]

0% found this document useful (0 votes)
18 views8 pages

Insert and Print The Elements in The Singly Linked List

The document contains Java code for implementing a singly linked list with functionalities to insert elements at the end, at the beginning, and at a specified position. It also includes a method to display the elements of the list and a solution for detecting cycles in a linked list. The code utilizes classes for nodes and the linked list itself, along with user input for data insertion.

Uploaded by

Sudheer Pavuluri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views8 pages

Insert and Print The Elements in The Singly Linked List

The document contains Java code for implementing a singly linked list with functionalities to insert elements at the end, at the beginning, and at a specified position. It also includes a method to display the elements of the list and a solution for detecting cycles in a linked list. The code utilizes classes for nodes and the linked list itself, along with user input for data insertion.

Uploaded by

Sudheer Pavuluri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Insert and print the elements in the singly linked list

import java.util.*;

class Node{

int data;

Node next;

Node(int data){

this.data = data;

this.next = null;

class Main{

public static void main (String[] args) {

Linkedlist list = new Linkedlist();

Scanner sc = new Scanner(System.in);

while(true){

int data = sc.nextInt();

if(data == -1){

list.display(data);

break;

list.insert(data);

}
class Linkedlist{

Node head;

public void insert(int data){

Node newNode = new Node(data);

if(head==null){

head = newNode;

}else{

Node last=head;

while(last.next!=null){

last=last.next;

last.next= newNode;

public void display(int data){

Node curr = head;

if(curr==null){

System.out.print("Empty");

}else{

while(curr!=null){

System.out.println(curr.data);

curr = curr.next;

}
}

Inserting an element at the beginning of a singly linked list:

import java.util.*;

class Node{

int data;

Node next;

Node(int data){

this.data = data;

this.next = null;

class Main{

public static void main (String[] args) {

Linkedlist list = new Linkedlist();

Scanner sc = new Scanner(System.in);

int data;

while(true){

data = sc.nextInt();

if(data == -1){

break;

list.insert(data);
}

int in = sc.nextInt();

list.insertfirst(in);

list.display(data);

class Linkedlist{

Node head;

public void insert(int data){

Node newNode = new Node(data);

if(head==null){

head = newNode;

}else{

Node last=head;

while(last.next!=null){

last=last.next;

last.next= newNode;

public void display(int data){

Node curr = head;

if(curr==null){

System.out.print("Empty");
}else{

while(curr!=null){

System.out.print(curr.data+" ");

curr = curr.next;

public void insertfirst(int in){

Node n = new Node(in);

n.next = head;

head = n;

Inserting the element in the singly linked list at a given position


import java.util.*;
class Node{
int data;
Node next;
Node(int data){
this.data = data;
this.next = null;
}
}
class Main{
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
Linkedlist list = new Linkedlist();
int data;
while(true){
data = sc.nextInt();
if(data == -1){
break;
}
list.insert(data);
}
int in = sc.nextInt();
int pos = sc.nextInt();
list.insertatposition(in,pos);
list.display(data);
}
}
class Linkedlist{
Node head;
public void insert(int data){
Node newNode = new Node(data);
if(head==null){
head = newNode;
}else{
Node last=head;
while(last.next!=null){
last=last.next;
}
last.next= newNode;
}
}
public void display(int data){
Node curr = head;
if(curr==null){
System.out.print("Empty");
}else{
while(curr!=null){
System.out.print(curr.data+" ");
curr = curr.next;
}
}
}
public void insertatposition(int in,int pos){
Node newnode = new Node(in);
Node temp = head;
for(int i=1;i<pos-1;i++){
temp = temp.next;
}
newnode.next = temp.next;
temp.next = newnode;
}
}

Leetcode problem 141


public class Solution {
public boolean hasCycle(ListNode head) {
ListNode fast = head;
ListNode slow = head;
while(fast!=null && fast.next!=null){
fast = fast.next.next;
slow = slow.next;
if(slow==fast)
return true;
}
return false;
}
}

You might also like