203.移除链表元素
题目链接:203.移除链表元素
由于一开始的代码基础链表部分花的功夫比较多,很容易就做出来了,只需要将指针指向下下一个元素(忽略下一个元素)就可以了,同时学习了虚拟头节点,删除时链表节点时使用。
代码如下:
普通删除法:
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def removeElements(self, head, val):
"""
:type head: Optional[ListNode]
:type val: int
:rtype: Optional[ListNode]
"""
while head != None and head.val == val:
head = head.next
cur = head
while cur!=None and cur.next != None:
if cur.next.val == val:
cur.next = cur.next.next
else:
cur = cur.next
return head
虚拟头节点删除法:
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution(object):
def removeElements(self, head, val):
"""
:type head: Optional[ListNode]
:type val: int
:rtype: Optional[ListNode]
"""
damn = ListNode()
damn.next = head
cur = damn
while cur.next != None:
if cur.next.val == val:
cur.next = cur.next.next
else:
cur = cur.next
return damn.next
707.设计链表
题目链接:707.设计链表
每个函数都互相影响,有一点小错就得疯狂找bug,最后对照着答案修改的代码。感觉链表逻辑不难,但是比较绕。需要多画图来直观确定逻辑再来写代码。
代码如下:
class Linklist:
def __init__(self,val=0,next=None):
self.val = val
self.next = next
class MyLinkedList(object):
def __init__(self):
self.damn_head = Linklist()
self.length = 0
def get(self, index):
"""
:type index: int
:rtype: int
"""
if index < 0 or index >= self.length:
return -1
current = self.damn_head.next
for i in range(index):
current = current.next
return current.val
def addAtHead(self, val):
"""
:type val: int
:rtype: None
"""
self.damn_head.next = ListNode(val, self.damn_head.next)
self.length += 1
def addAtTail(self, val):
"""
:type val: int
:rtype: None
"""
current = self.damn_head
while current.next:
current = current.next
current.next = ListNode(val)
self.length += 1
def addAtIndex(self, index, val):
"""
:type index: int
:type val: int
:rtype: None
"""
if index < 0 or index > self.length:
return
current = self.damn_head
for i in range(index):
current = current.next
current.next = ListNode(val, current.next)
self.length += 1
def deleteAtIndex(self, index):
"""
:type index: int
:rtype: None
"""
if index < 0 or index >= self.length:
return
current = self.damn_head
for i in range(index):
current = current.next
current.next = current.next.next
self.length -= 1
# Your MyLinkedList object will be instantiated and called as such:
# obj = MyLinkedList()
# param_1 = obj.get(index)
# obj.addAtHead(val)
# obj.addAtTail(val)
# obj.addAtIndex(index,val)
# obj.deleteAtIndex(index)
206.反转链表
题目链接:206.反转列表
以为很难,直接去看了视频,但是并不难,自己看完视频一遍过。主要是递归写法,理解的不够深刻,还好之前有C语言递归的基础能浅层理解过来。多画图!!!:(
代码如下:
双指针法:
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def reverseList(self, head):
"""
:type head: Optional[ListNode]
:rtype: Optional[ListNode]
"""
cur = head
pre = None
while cur:
temp = cur.next
cur.next = pre
pre = cur
cur = temp
return pre
递归写法:
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
def reverse(cur,pre):
if cur == None:
return pre
temp = cur.next
cur.next = pre
return reverse(temp,cur)
class Solution(object):
def reverseList(self, head):
"""
:type head: Optional[ListNode]
:rtype: Optional[ListNode]
"""
return reverse(head,None)
总结:链表一开始感觉很上强度,但是真正上手后感觉很有意思,也能理解进去,相对好想思路,但是还得多加练习,尽量不看视频想出思路。
参考:
题目链接/文章讲解/视频讲解::代码随想录