LinkedList 7 removeLast()

Поділитися
Вставка
  • Опубліковано 5 жов 2024
  • Dr. Rob Edwards from San Diego State University describes the removeLast() method that is always O(n)

КОМЕНТАРІ • 17

  • @codify4288
    @codify4288 6 років тому +19

    Hello, we don't need two pointers...
    public E removeLast() {
    if (tail == null)
    return null;
    E data = tail.data;
    if (tail == head)
    tail = head = null;
    else {
    Node temp = head;
    while (temp.next != tail)
    temp = temp.next;
    tail = temp;
    tail.next = null;
    }
    return data;
    }

    • @FerreiraVitor
      @FerreiraVitor 4 роки тому +1

      Using tmp.next.next works fine as well once you're sure your list has two or more itens. What we've just done!
      public E removeLast() {
      if(head == null) return null;
      E data = tail.data;

      if(head == tail) {
      head = tail = null;
      } else {
      Node tmp = head;
      while(tmp.next.next != null) {
      tmp = tmp.next;
      }
      tail = tmp;
      tail.next = null;
      }

      currentSize--;
      return data;
      }

  • @Hanhchi89
    @Hanhchi89 4 роки тому +3

    Thank you so much. Your students are so lucky to have you as their professor.

  • @saraye9336
    @saraye9336 5 років тому +5

    The BEST explanation of data-structures. Thx so much! Add more topics from computer-science plz :)

  • @olzhasdossaibek1642
    @olzhasdossaibek1642 4 роки тому +1

    Can't help but like these videos because they teach me quite a lot. Professor's own habits and jokes teach me when the material fails to

  • @leonazraev4902
    @leonazraev4902 6 років тому +21

    the line "previous.next = tail" dont need to be "previous.next = null" ?

    • @ziador124
      @ziador124 5 років тому +2

      That confused me too!

    • @ziador124
      @ziador124 5 років тому +7

      lol he updated it at 11:26

    • @motivational_writer53
      @motivational_writer53 4 роки тому +4

      thx for clarifying this! we teach each other

    • @lastidea4925
      @lastidea4925 2 роки тому +1

      I stuck there too. Stopped the video and thought for minutes what's the purpose of that lol

  • @sivapcu
    @sivapcu 7 років тому +1

    All your lectures are awesome...

  • @fawziaha.9846
    @fawziaha.9846 6 років тому

    This's so helpful,many thanks

  • @giorgi23
    @giorgi23 4 роки тому

    How about have previous and next Nodes into your node class?
    tail = tail.previous;
    tail.next = null;

  • @thomassun3046
    @thomassun3046 3 роки тому +1

    at 9:40, what if previous.next == tail is removed? is it necessary?

    • @thomassun3046
      @thomassun3046 3 роки тому

      I'm confused at 9:40, but at 11:22, processor found the issue when checking the end condition, so no issue now!

  • @user102-j1t
    @user102-j1t Рік тому

    is...he writing in reverse?

  • @Mohamedreda-nx5fu
    @Mohamedreda-nx5fu 6 років тому

    great