今天看了数据结构中的递归,一般链表的题都可以用到递归来实现。
所以这道题用递归的方法来试试:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeElements(self, head: ListNode, val: int) -> ListNode:
if head is None:
return head
print(head)
# removeElement方法会返回下一个Node节点
head.next = self.removeElements(head.next, val)
print(head)
if head.val == val:
next_node = head.next
else:
next_node = head
print(head)
print(next_node)
print("————————————分割线——————————")
return next_node
才开始不理解这样,后面加了打印输出来看整个过程:
ListNode{val: 1, next: ListNode{val: 2, next: ListNode{val: 6, next: ListNode{val: 3, next: ListNode{val: 4, next: ListNode{val: 5, next: ListNode{val: 6, next: None}}}}}}}
ListNode{val: 2, next: ListNode{val: 6, next: ListNode{val: 3, next: ListNode{val: 4, next: ListNode{val: 5, next: ListNode{val: 6, next: None}}}}}}
ListNode{val: 6, next: ListNode{val: 3, next: ListNode{val: 4, next: ListNode{val: 5, next: ListNode{val: 6, next: None}}}}}
ListNode{val: 3, next: ListNode{val: 4, next: ListNode{val: 5, next: ListNode{val: 6, next: None}}}}
ListNode{val: 4, next: ListNode{val: 5, next: ListNode{val: 6, next: None}}}
ListNode{val: 5, next: ListNode{val: 6, next: None}}
ListNode{val: 6, next: None}
ListNode{val: 6, next: None}
ListNode{val: 6, next: None}
None
————————————分割线——————————
ListNode{val: 5, next: None}
ListNode{val: 5, next: None}
ListNode{val: 5, next: None}
————————————分割线——————————
ListNode{val: 4, next: ListNode{val: 5, next: None}}
ListNode{val: 4, next: ListNode{val: 5, next: None}}
ListNode{val: 4, next: ListNode{val: 5, next: None}}
————————————分割线——————————
ListNode{val: 3, next: ListNode{val: 4, next: ListNode{val: 5, next: None}}}
ListNode{val: 3, next: ListNode{val: 4, next: ListNode{val: 5, next: None}}}
ListNode{val: 3, next: ListNode{val: 4, next: ListNode{val: 5, next: None}}}
————————————分割线——————————
ListNode{val: 6, next: ListNode{val: 3, next: ListNode{val: 4, next: ListNode{val: 5, next: None}}}}
ListNode{val: 6, next: ListNode{val: 3, next: ListNode{val: 4, next: ListNode{val: 5, next: None}}}}
ListNode{val: 3, next: ListNode{val: 4, next: ListNode{val: 5, next: None}}}
————————————分割线——————————
ListNode{val: 2, next: ListNode{val: 3, next: ListNode{val: 4, next: ListNode{val: 5, next: None}}}}
ListNode{val: 2, next: ListNode{val: 3, next: ListNode{val: 4, next: ListNode{val: 5, next: None}}}}
ListNode{val: 2, next: ListNode{val: 3, next: ListNode{val: 4, next: ListNode{val: 5, next: None}}}}
————————————分割线——————————
ListNode{val: 1, next: ListNode{val: 2, next: ListNode{val: 3, next: ListNode{val: 4, next: ListNode{val: 5, next: None}}}}}
ListNode{val: 1, next: ListNode{val: 2, next: ListNode{val: 3, next: ListNode{val: 4, next: ListNode{val: 5, next: None}}}}}
ListNode{val: 1, next: ListNode{val: 2, next: ListNode{val: 3, next: ListNode{val: 4, next: ListNode{val: 5, next: None}}}}}
————————————分割线——————————
其实就是说我的整个过程是先遍历整个链表,就像第一个分割线以上的除了倒数三行外;
接下来就是判断赋值语句,这个next_node相当于是head的一个指针吧!
当这个head.val == val成立的时候,我的这个指针还是指向的下一个节点,就像第一个分割线和第五个分割线之上的部分;
而当这个不成立时,将这个节点赋给next_node,但是这个时候的next_node的节点是之前的指针指向的节点加上这个不成立的新节点
第二种方法就是常规的迭代:
新增一个dummy节点, 方便遍历和最后返回结果
p 指针向后遍历, 向前探一个节点, 如果val相等, 则需要跳过 next 节点
class Solution:
def removeElements(self, head: ListNode, val: int) -> ListNode:
dummy = ListNode()
dummy.next = head
p = dummy
while p is not None:
# 向前探一个节点检查是否等于val
if p.next and p.next.val == val:
# 跳过 p.next 节点
p.next = p.next.next
else:
p = p.next
return dummy.next