1.删除链表的节点
编写一个函数,在给定单链表一个结点(非尾结点)的情况下,删除该结点。
假设该链表为1 -> 2 -> 3 -> 4
并且给定你链表中第三个值为3
的节点,在调用你的函数后,该链表应变为1 -> 2 -> 4
。
#Definition for singly-linked list.
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
class Solution(object):
def deleteNode(self, node):
"""
:type node: ListNode
:rtype: void Do not return anything, modify node in-place instead.
"""
node.val = node.next.val
node.next = node.next.next
2.删除倒数第N个节点
给定一个链表,删除链表的倒数第 n 个节点并返回头结点。
例如,
给定一个链表: 1->2->3->4->5, 并且 n = 2. 当删除了倒数第二个节点后链表变成了 1->2->3->5.
说明:
给的 n 始终是有效的。
尝试一次遍历实现。
思路:因为单向链表是没有前驱指针的,所以应找到倒数第n+1个结点;n有可能等于链表的长度,故先new一个head的前驱结点fakeHead。用两个指针slow、fast从fakeHead开始,先移动fast n步,使得其距离slow为n;然后,两个指针同步移动,当fast.next走到None时,即fast走到最后一个节点时,slow即处于倒数第n+1个结点(倒数第n+1个节点,与最后一个节点下标差为n,这也是为什么fast一开始要跑n步),删除slow的next结点即可。
class Solution(object):
def removeNthFromEnd(self, head, n):
"""
:type head: ListNode
:type n: int
:rtype: ListNode
"""
fakehead = ListNode(0)
fakehead.next = head
slow = fakehead
fast = fakehead
for i in range(n):
fast = fast.next
while fast.next:
fast = fast.next
slow =slow.next
slow.next = slow.next.next
return fakehead.next
3.反转一个链表(迭代)
思路:单链表的首端操作为O(1),故考虑在首端一个一个取下链表头,然后重新连接在一起,即完成了反正操作
class Solution(object):
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
rev = None
while head is not None:
tmp = head
head = tmp.next
tmp.next = rev
rev = tmp
return rev
3.反转一个链表(递归)
def rec(head):
"""
:type head: ListNode
:rtype: ListNode
"""
if head == None or head.next == None:
return head
rev = rec(head.next)
head.next.next = head
head.next = None
return rev
此题参见:https://blog.csdn.net/fx677588/article/details/72357389
4.合并两个有序列表
合并两个已排序的链表,并将其作为一个新列表返回。新列表应该通过拼接前两个列表的节点来完成。
示例:
输入:1->2->4, 1->3->4 输出:1->1->2->3->4->4
class Solution(object):
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
newhead = ListNode(-1)
i, j, p = l1, l2, newhead
while i or j:
if i is not None:
if j is not None and i.val > j.val:
p.next = j
j = j.next
else:
p.next = i
i = i.next
else:
p.next = j
j = j.next
p = p.next
return newhead.next
5.回文链表
请检查一个链表是否为回文链表。
进阶:
你能在 O(n) 的时间和 O(1) 的额外空间中做到吗?
5.1思路:用两个指针分别跑,找到中间位置,然后将后面依次反转,再从头开始与后面比较
class Solution(object):
def isPalindrome(self, head):
"""
:type head: ListNode
:rtype: bool
"""
slow, fast = head, head
if fast == None or fast.next == None:
return True
#considering even and odd condition
#odd:slow is the mid,from slow.next reverse,fast.next is the ending symbol
#even:slow is the first mid,from slow.next reverse,fast.next.next is the ending symbol
while fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next
#from slow.next reverse
i = slow.next
res = None
while i:
tmp = i
i = i.next
tmp.next = res
res = tmp
slow.next = res
#
ps = head
pf = slow.next
while pf:
if ps.val != pf.val:
return False
ps=ps.next
pf = pf.next
return True
5.2上面跑过后,思考是否能利用第一次跑中间位置的循环,顺便把低区反转了,答案是可以的。(顺便把代码精简一下,显得更加python一点~哈哈)
class Solution(object):
def isPalindrome(self, head):
"""
:type head: ListNode
:rtype: bool
"""
if head == None or head.next == None:
return True
##定义低区,高区指针,recnct为记忆头部位置,完成反转后用于重新与高区链接
recnct,slow, fast = head, head, head
newhead = None
while fast and fast.next:
#边跑边将前半段反转
fast = fast.next.next
tmp = slow
slow = slow.next
tmp.next = newhead
newhead = tmp
#将前半段反转后重新链接起来
recnct.next = slow
#定义地位指针ps,高位指针pf
ps = newhead
pf = slow.next if fast else slow #fast不为NONE,奇数情况;fast为None,偶数情况
#依次判断,如存在不一样则返回False
while pf:
if ps.val != pf.val:
return False
ps = ps.next
pf = pf.next
#如果前面没有返回,证明是回文
return True