Skip to main content

Posts

Showing posts with the label linkedlist

Finding a loop in a linked List

Finding a loop in a linked List:- public class LinkedListLoop { //Two pointers public Node loopCheck ( Node head ) { //fast pointer take two steps at a time Node fastptr = head ; //slow pointer take one step at a time Node slowptr = head ; if ( head == null ) { System . out . println ( "no loops empty list" ); } //Till any pointer becomes null while ( slowptr != null && fastptr != null && fastptr . next != null ) { slowptr = slowptr . next ; fastptr = fastptr . next . next ; //if both pointers point to same node //possible only in loop list if ( slowptr == fastptr ) { System . out . println ( "loopfound" ); fastptr = head ; while ( fastptr . next != slowptr ) { fastptr = fastptr . next ; } return fastptr ; } } System . out . println ( "loop not found" ); return head ; } public void makeCycle ( N...

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 ; }

Delete node at a given position in Linked List

Delete node at a given position in Linked List //delete node by position in linked list public Node deleteKeyAtPosition ( Node head , int position ) { Node temp = head ; Node prevtemp = temp ; int c = 1 ; //if position is head if ( position == 1 ) { head = head . next ; return head ; } //position +1 because we have to go till that point while ( c != position + 1 ) { if ( c == position && position != 1 ) { prevtemp . next = temp . next ; temp . next = null ; temp = prevtemp ; } prevtemp = temp ; temp = temp . next ; c ++; } return head ; }

Deleting a Node by passing data in LInkedList

Deleting a Node by passing data in LInkedList:- The node can be deleted by passing the data value in a function.Here we have three pointers one to traverse the list other to point current node and third one to point previous of current node. //deleting a given key public Node deleteKeyNode ( Node head , int key ) { //traverse pointer Node temp = head ; //previous pointer Node prevtemp = temp ; while ( temp != null ) { //when key is at head position if ( key == head . data ) { head = head . next ; System . out . println ( "head is deleted" ); return head ; } //when key is at any position other than head if ( temp . data == key ) { prevtemp . next = temp . next ; temp . next = null ; temp = prevtemp ; } prevtemp = temp ; temp = temp . next ; } return head ; }

Adding Node at a given position in Linked List

Adding Node at a given position in Linked List public void addNodeAtPosition ( Node head , int position , int data ) { Node temp = head ; //create a node Node node = new Node ( data ); int c = 1 ; //while counter is not equal to the position while ( c != position ) { temp = temp . next ; c ++; } //point node to the next of current node node . next = temp . next ; //point next of current to the node to insert temp . next = node ; } To Know the basic structure of the linked list click  here

Adding Node at the End of Linked List

Adding Node at the End of Linked List:- The method takes two parameter one a head node of a linked list and other data too insert. public void addNodeEnd ( Node head , int data ) { if ( head == null ) { System . out . println ( "list is empty" ); return ; } Node node = new Node ( data ); Node temp = head ; while ( temp . next != null ) { temp = temp . next ; } temp . next = node ; } To Know the basic structure of the linked list click here

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 = no...

.