删除链表中等于给定值val的所有节点(python实现)


Example:

Given 1->2->3->4->5->3, val = 3, return the list as 1->2->4->5

 1 # Definition for singly-linked list.
 2 # class ListNode:
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.next = None
 6 
 7 class Solution:
 8     # @param head, a ListNode
 9     # @param val, an integer
10     # @return a ListNode
11     def removeElements(self, head, val):
12         # Write your code here
13         if head == None:
14             return head
15         dummy = ListNode(0)
16         dummy.next = head
17         pre = dummy
18         while head:
19             if head.val == val:
20                 pre.next = head.next
21                 head = pre
22             pre = head
23             head = head.next
24         return dummy.next

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM