链表
2021-01-04 本文已影响0人
warManHy
- 链表概念
- 链表建立
class ListNode(object):
def __init__(self, val=0, next=None):
self.val = val
self.next = next
listnode1 = new ListNode(-1)
listnode2 = new ListNode(-1)
- 查,删,增
编写程序以 x 为基准分割链表,使得所有小于 x 的节点排在大于或等于 x 的节点之前。如果链表中包含 x,x 只需出现在小于 x 的元素之后(如下所示)。分割元素 x 只需处于“右半部分”即可,其不需要被置于左右两部分之间
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/partition-list-lcci
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class ListNode(object):
def __init__(self, val=0, next=None):
self.val = val
self.next =next
listnode1 = ListNode(-1)
listnode2 = ListNode(-1)
dummy1 = listnode1
dummy2 = listnode2
p = head
while p != None:
if dummy1.val < x:
dummy1.next = p
dummy1 = dummy1.next
else:
dummy2.next = p
dummy2 = dummy2.next
p = p.next
if listnode1 == None:
return head
else:
dummy1.next = listnode2.next
dummy2.next = None
return dummy1.next
- 反转
https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/ - 排序
https://leetcode-cn.com/problems/insertion-sort-list/ - 环,相交,合并
https://leetcode-cn.com/problems/intersection-of-two-linked-lists/
https://leetcode-cn.com/problems/middle-of-the-linked-list/
合并:
https://leetcode-cn.com/problems/merge-two-sorted-lists/ - 分割