刪除鏈表中等於給定值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