Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'size()' method in linked list data structure and also written te… #1047

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bhaveshmhadse
Copy link

Update file src/data-structures/linked-list/LinkedList.js and src/data-structures/linked-list/test/LinkedList.test.js

  • src/data-structures/linked-list/LinkedList.js

Before

After

   /**
   * @returns {Number}
   */
  size() {
    // If head is null the size of linked list will be -1 or 0, So here we return -1.
    if (!this.head) {
      return -1;
    }

    let size = 0;
    let currentNode = this.head;

    // Traverse to the linked list and update the size.
    while (currentNode) {
      size += 1;
      currentNode = currentNode.next;
    }

    return size;
  }
  • src/data-structures/linked-list/test/LinkedList.test.js

Before

After

  it('should return the size of linked list', () => {
    const linkedList = new LinkedList();

    linkedList.append(1);
    linkedList.append(2);
    linkedList.append(3);

    expect(linkedList.size()).toBe(3);
  });

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
…st case for it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant