Skip to content

Commit

Permalink
Merge pull request laviii123#235 from kushal34712/patch-4
Browse files Browse the repository at this point in the history
Create squdrequst799
  • Loading branch information
laviii123 authored Oct 8, 2023
2 parents 0315dba + 47cd92c commit 0598911
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions squdrequst799
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
////



class Solution {
Node sortedInsert(Node head1, int key) {
// Step 1: Create a new node with the given key.
Node newNode = new Node(key);

// If the linked list is empty or the key is smaller than or equal to the head's data,
// insert the new node at the beginning.
if (head1 == null || key <= head1.data) {
newNode.next = head1;
return newNode;
}

// Step 2: Traverse the linked list to find the correct position.
Node current = head1;
Node prev = null;
while (current != null && current.data < key) {
prev = current;
current = current.next;
}

// Step 3: Insert the new node at the correct position.
prev.next = newNode;
newNode.next = current;

// Step 4: Return the head of the modified linked list.
return head1;
}
}

0 comments on commit 0598911

Please sign in to comment.