LeetCode--203--删除链表中的节点

问题描述:

删除链表中等于给定值 val 的所有节点。

示例:

输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5

方法1:防止[1,1,1,1] 1 用while head。

 1 class Solution(object):
 2     def removeElements(self, head, val):
 3         """
 4         :type head: ListNode
 5         :type val: int
 6         :rtype: ListNode
 7         """
 8         if head == None:
 9             return 
10         while head and  head.val == val:
11             head = head.next
12         p = head
13         while p:
14             if p.next and p.next.val == val:
15                 p.next = p.next.next
16         
17             else:
18                 p = p.next
19         return head

2018-09-17 19:34:21

猜你喜欢

转载自www.cnblogs.com/NPC-assange/p/9664377.html