Leetcode Remove Linked list Elements Delete linked list elements

Source: Internet
Author: User

Test instructions: Removes all elements in the linked list that have an element value of Val.

Idea: The algorithm complexity is definitely O (n), then the pursuit of less code and less additional operations. I can't do it.

1 /**2 * Definition for singly-linked list.3 * struct ListNode {4 * int val;5 * ListNode *next;6 * ListNode (int x): Val (x), Next (NULL) {}7  * };8  */9 classSolution {Ten  Public: Onelistnode* removeelements (listnode* head,intval) { A          while(Head&&head->val==val) head=head->Next; -         if(!head)return 0; -ListNode *tmp=head; the          while(head&&head->next) -         { -             if(head->next->val==val) -Head->next=head->next->Next; +             Else -Head=head->Next; +         } A         returntmp; at     } -};
Remove Linked List Elements

Leetcode Remove Linked list Elements Delete linked list elements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.