203. Remove Linked List Elements [Easy] (Python)

Source: Internet
Author: User

Topic links

https://leetcode.com/problems/remove-linked-list-elements/

Original title

Remove all elements from a linked list of integers, that has value Val.

Example
given:1–> 2–> 6–> 3–> 4–> 5–> 6, val = 6
Return:1–> 2–> 3–> 4–> 5

Topic translation

Deletes the node in the single-linked list for the given Val. For example: Given list 1–> 2–> 6–> 3–> 4–> 5–> 6 and value val = 6, return 1–> 2–> 3–> 4–> 5.

Method of Thinking Idea one

Traversing all nodes while preserving the previous node of each node, delete the node when it encounters a value of Val. For ease of operation, a pseudo-head node is defined.

Code

# Definition for singly-linked list.# class ListNode (object):# def __init__ (self, x):# self.val = x# self.next = None class solution(object):     def removeelements(self, head, Val):        "" : Type Head:ListNode:type val:int:rtype:ListNode "" "New_head = Pre = ListNode (0) Pre.next = Head whileHeadifHead.val = = Val:pre.next = Head.nextElse: Pre = Pre.next head = Head.nextreturnNew_head.next
Idea two

The topic does not require the value of the node not to change, then you can define the speed two pointers, when the fast pointer over the linked list, the value of the node is not equal to the Val value of the node is assigned to the slow pointer point.

Code

# Definition for singly-linked list.# class ListNode (object):# def __init__ (self, x):# self.val = x# self.next = None class solution(object):     def removeelements(self, head, Val):        "" : Type Head:ListNode:type val:int:rtype:ListNode "" "New_head = ListNode (0) New_head.next = head slow, fast = New_head, head whileFastifFast.val! = Val:slow.next.val = Fast.val Slow = slow.next Fast = Fast.next Slow.next =None        returnNew_head.next

Description
The above solution is used to dummy node, in fact, you can start from the head of the Val-worthy node removal, so that no additional nodes are required.
In addition, the problem can be handed back to do, the code is simple but not ac ... But in C language to write the same code can be AC, so write a bit below is to provide ideas, for reference only.

Code (recursive, non-AC)

# Definition for singly-linked list.# class ListNode (object):# def __init__ (self, x):# self.val = x# self.next = None class solution(object):     def removeelements(self, head, Val):        "" : Type Head:ListNode:type val:int:rtype:ListNode "" "        if  notHeadreturnHead Head.next = self.removeelements (Head.next, Val)returnHeadifHead.val! = ValElseHead.next

PS: Novice Brush Leetcode, new handwritten blog, write wrong or write not clear also please help point out, thank you!
Reprint Please specify: http://blog.csdn.net/coder_orz/article/details/51706294

203. Remove Linked List Elements [Easy] (Python)

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.