Wednesday, January 17, 2024

Merge two sorted linked lists


Merge two sorted linked lists


public class MergeLinkedLists {

static class ListNode {

int val;

ListNode next;

public ListNode() {}


public ListNode(int val) {

this.val = val;

}


public ListNode(int val, ListNode next) {

this.val = val;

this.next = next;

}

}

public static ListNode mergeTwoLists(ListNode list1, ListNode list2) {

ListNode mergedList = new ListNode();

ListNode current = mergedList;


while (list1 != null && list2 != null) {

if (list1.val <= list2.val) {

current.next = list1;

list1 = list1.next;

} else {

current.next = list2;

list2 = list2.next;

}

current = current.next;

}


if (list1 != null) {

current.next = list1;

} else {

current.next = list2;

}


return mergedList.next;

}


public static void main(String[] args) {

// Example usage:

ListNode list1 = new ListNode(1, new ListNode(2, new ListNode(4)));

ListNode list2 = new ListNode(1, new ListNode(3, new ListNode(4)));


ListNode mergedList = mergeTwoLists(list1, list2);

while (mergedList != null) {

System.out.print(mergedList.val + " ");

mergedList = mergedList.next;

}

}

}


Time Complexity: O(M + N), Where M and N are the size of the list1 and list2 respectively.
Auxiliary Space: O(M+N), Function call stack space


Output:


1 1 2 3 4 4


Geeks link:

    Merge To Linked List


8 comments:

  1. Eric Emanuel shorts represent a lifestyle tied to confidence and individuality. They’ve been embraced by athletes, artists, and streetwear fans alike, becoming a symbol of relaxed luxury and sports-inspired fashion.

    ReplyDelete
  2. There’s a raw, emotional edge to Broken Planet Market that sets it apart in the streetwear space. It feels like a brand that understands its audience and reflects how people actually feel, not just what’s trending.

    ReplyDelete
  3. Winter sales make Denim Tears jackets more accessible. Definitely a good option for cold-season fashion. I love how Denim Tears jackets can be worn casually or styled up. Perfect for winter outings.

    ReplyDelete
  4. Corteiz joggers have a clean streetwear vibe for winter. They look comfortable for daily wear. The winter sale timing is perfect.

    ReplyDelete
  5. I like how the Chrome Hearts hat adds a modern twist to classic streetwear. It’s bold yet balanced. A great piece.

    ReplyDelete
  6. Browsing through newsswing com has been a wonderful experience. The content is always fresh, informative, and relevant, which makes newsswing com stand out as a trustworthy source for news enthusiasts seeking detailed and accurate updates daily.

    ReplyDelete
  7. Tech enthusiasts will find techactually com to be a treasure trove of information. From gadget reviews to tech tips, every piece of content on techactually is reliable, comprehensive, and highly relevant to anyone passionate about technology.

    ReplyDelete
  8. The editorial team behind this platform does a fantastic job, and newsbrave continually impresses with credible reporting, exclusive updates, and engaging stories that reflect careful research and an unwavering commitment to quality journalism.

    ReplyDelete

Create a Digital Clock using HTML and JavaScript

Create a Digital Clock using HTML and JavaScript  <! DOCTYPE html> < html > < head > ...

Followers

Search This Blog

Popular Posts