Linked List Problems in Java - 40 : Swap nodes of given values without swapping data

Поделиться
HTML-код
  • Опубликовано: 10 окт 2024
  • Source Code: thecodingsimpl...
    In this video, we're going to reveal exact steps to Swap nodes of given values without swapping data in linked list in Java
    CHECK OUT CODING SIMPLIFIED
    / codingsimplified
    ★☆★ VIEW THE BLOG POST: ★☆★
    thecodingsimpli...
    I started my RUclips channel, Coding Simplified, during Dec of 2015.
    Since then, I've published over 200+ videos.
    ★☆★ SUBSCRIBE TO ME ON RUclips: ★☆★
    www.youtube.co...
    ★☆★ Send us mail at: ★☆★
    Email: thecodingsimplified@gmail.com
    This Problem is synonym of following problems:
    Swap nodes of given values without swapping data in linked list in Java,
    Swap nodes of given values without swapping data in linked list,
    how to Swap nodes of given values without swapping data in linked list,
    swap nodes in linked list,
    coding simplified,
    linked list,
    java,

Комментарии • 10

  • @gopnik9567
    @gopnik9567 2 года назад

    So for when they are adjacent to each other code goes like this ?
    /* code where you check if the prev is null or not */
    if(prev1 == t2) {
    t2.next = t1.next;
    t1.next = t2;
    return head;
    } else if(prev2 == t1) {
    t1.next = t2.next;
    t2.next = t1.next;
    return head;
    }
    /* code with tmp */

  • @anugarg3872
    @anugarg3872 5 лет назад

    I don't think so it will work if nodes to be swapped are adjacent nodes.

  • @AdityaSharma-hy9iv
    @AdityaSharma-hy9iv 3 года назад

    You have missed out a case where the elements are adjacent to each other.

    • @CodingSimplified
      @CodingSimplified  3 года назад

      Thanks for answering in your other comment. I'll check it further.

    • @helloUser12373
      @helloUser12373 2 года назад

      @@CodingSimplified The Code is correct (I have run a simlar code on gfg), this is handling the adjacent cases also.

    • @gamingwithasmile268
      @gamingwithasmile268 Год назад

      @@helloUser12373 chup bilkul chup

  • @fazilshafibhat2751
    @fazilshafibhat2751 2 года назад

    //Code for adjacent nodes:
    if(j == i+1){
    LinkedListNode prev = null;
    LinkedListNode c1 = null;
    LinkedListNode c2 = null;
    int x = 0;
    int y = 1;
    while(x

  • @AdityaSharma-hy9iv
    @AdityaSharma-hy9iv 3 года назад

    For anyone who needs help where the nodes are adjacent to each other, here is a small snippet:
    ListNode firstNode = head;
    ListNode secondNode= firstNode.next;
    head=secondNode;
    secondNode.next=firstNode;
    firstNode.next=null;
    return head;

    • @helloUser12373
      @helloUser12373 2 года назад

      The Code is correct (I have run a similar code on gfg), this is handling the adjacent cases also.

    • @gamingwithasmile268
      @gamingwithasmile268 Год назад

      @@helloUser12373 chup bilkul chup