Skip to main content

Posts

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

.