File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -74,7 +74,64 @@ public void insertNth(int data, int position) {
7474 cur .next = newNode ;
7575 size ++;
7676 }
77-
77+
78+ /**
79+ Detects if there is a loop in the singly linked list
80+ using floy'd turtle and hare algorithm.
81+ **/
82+ public boolean detectLoop (){
83+ Node currentNodeFast = head ;
84+ Node currentNodeSlow = head ;
85+ boolean flag = false ;
86+ while (currentNodeFast !=null && currentNodeFast .next != null && currentNodeSlow !=null && currentNodeSlow .next != null ){
87+ currentNodeFast = currentNodeFast .next .next ;
88+ currentNodeSlow = currentNodeSlow .next ;
89+ if (currentNodeFast ==currentNodeSlow ){
90+ flag = true ;
91+ break ;
92+ }
93+ }
94+ return flag ;
95+ }
96+
97+ /**
98+ Swaps nodes of two given values a and b.
99+ **/
100+
101+ public void swapNodes (int a , int b ){
102+ Node currentNode = head ;
103+ Node temp = null ;
104+ while (currentNode !=null ){
105+ if (currentNode .next .value == a ){
106+ temp = currentNode .next ;
107+ }
108+ if (currentNode .next .value == b ){
109+ currentNode .next =temp ;
110+ }
111+ currentNode =currentNode .next ;
112+ }
113+ }
114+
115+
116+ /**
117+ Reverse a singly linked list from a given node till the end
118+ **/
119+
120+
121+ Node reverseList (Node node ) {
122+ Node prev = null , curr = node , next ;
123+ while (curr != null ) {
124+ next = curr .next ;
125+ curr .next = prev ;
126+ prev = curr ;
127+ curr = next ;
128+ }
129+ node = prev ;
130+ return node ;
131+ }
132+
133+
134+
78135 /** Deletes a node at the head */
79136 public void deleteHead () {
80137 deleteNth (0 );
You can’t perform that action at this time.
0 commit comments