LeetCode初级算法的Python实现--链表
2020-02-02 10:46
781 查看
LeetCode初级算法的Python实现--链表
之前没有接触过Python编写的链表,所以这里记录一下思路。这里前面的代码是和leetcode中的一样,因为做题需要调用,所以下面会给出。
首先定义链表的节点类。
# 链表节点 class ListNode(object): def __init__(self, x): self.val = x # 节点值 self.next = None
其次分别定义将列表转换成链表和将链表转换成字符串的函数;
# 将列表转换成链表 def stringToListNode(input): numbers = input dummyRoot = ListNode(0) ptr = dummyRoot for number in numbers: ptr.next = ListNode(number)# 分别将列表中每个数转换成节点 ptr = ptr.next ptr = dummyRoot.next return ptr # 将链表转换成字符串 def listNodeToString(node): if not node: return "[]" result = "" while node: result += str(node.val) + ", " node = node.next return "[" + result[:-2] + "]"
leetcode初级算法链表相关代码如下:
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 # print(listNodeToString(node)) # 删除链表的倒数第N个节点 def removeNthFromEnd(self, head, n): """ :type head: ListNode :type n: int :rtype: ListNode """ listNode = [] while head:# 将每个节点存放在列表中 listNode.append(head) head = head.next if 1 <= n <= len(listNode):# 如果n在列表个数之内的话 n = len(listNode) - n# n原本是倒数位置,现在赋值为正方向位置 if n == 0:# 如果是删除第1个位置的节点 if len(listNode) > 1:# 如果节点总数大于1 listNode[0].val = listNode[1].val# 删除第1个位置 listNode[0].next = listNode[1].next else: return None# 因为节点一共就1个或0个,所以删除1个直接返回None else: listNode[n - 1].next = listNode .next# 将该节点的上一个节点的后节点赋值为该节点的后节点,即删除该节点 return listNode[0] # 反转链表 def reverseList(self, head): """ :type head: ListNode :rtype: ListNode """ listNode = [] while head: listNode.append(head) head = head.next if len(listNode) == 0: return None for i in range(int(len(listNode) / 2)):# 将节点的值收尾分别调换 listNode[i].val, listNode[len(listNode) - i - 1].val = listNode[len(listNode) - i - 1].val, listNode[i].val return listNode[0] # 合并两个有序链表 def mergeTwoLists(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ newList = ListNode(0) newList.next = l1 prev= newList# 获得新链表 while l2: if not l1:# 如果l1不存在,直接返回l2即可 prev.next = l2 break if l1.val > l2.val:# 1,判断l1和l2哪个大,如果l2小,则将新节点的后面设为l2的头节点,并将头节点的后面设置为l1,反之l1小,则直接将头节点的后面设置为l1,并将节点后移 temp = l2 l2 = l2.next prev.next = temp temp.next = l1 prev = prev.next# else:# 反之l2大于l1,则是l1节点向后移 l1, prev = l1.next, l1 return newList.next # 回文链表 def isPalindrome(self, head): """ :type head: ListNode :rtype: bool """ listNode = [] while head: listNode.append(head) head = head.next for i in range(int(len(listNode) / 2)):# 判断两头的值是否一样大 if listNode[i].val != listNode[len(listNode) - i - 1].val: return False return True # 环形链表 def hasCycle(self, head): """ :type head: ListNode :rtype: bool """ if not head: return False p1=p2=head while p2.next and p2.next.next:# p1走1步,p2走两步,如果在链表没走完的情况下,找到完全相同的节点,就是找到环了 p1=p1.next p2=p2.next.next if p1==p2: return True return False head = [1,2,3,4,5] head2 = [4, 5, 8, 9] s = Solution() # print(s.deleteNode(stringToListNode(head))) # print(listNodeToString(s.removeNthFromEnd(stringToListNode(head), 1))) # 删除倒数第一个位置 # print(listNodeToString(s.reverseList(stringToListNode(head)))) # 翻转 # print(listNodeToString(s.mergeTwoLists(stringToListNode(head2), stringToListNode(head)))) # 合并两个链表 # print(s.isPalindrome(stringToListNode(head))) # print(s.hasCycle(stringToListNode(head)))
转载于:https://www.cnblogs.com/NSGUF/p/9157903.html
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- LeetCode探索模块初级算法链表章节python3代码实现
- LeetCode探索模块初级算法数组章节python3代码实现
- LeetCode初级算法的Python实现--排序和搜索、设计问题、数学及其他
- LeetCode探索模块初级算法字符串章节python3代码实现
- LeetCode初级算法的Python实现--动态规划
- [算法Rust,Go,Python,JS实现)]LeetCode之21-合并两个有序链表
- 【LeetCode-面试算法经典-Java实现】【082-Remove Duplicates from Sorted List II(排序链表中删除重复元素II)】
- 算法之python创建链表实现cache
- 【LeetCode-面试算法经典-Java实现】【138-Copy List with Random Pointer(拷贝有随机指针的单链表)】
- 【LeetCode】Python实现-237. 删除链表中的节点
- 【LeetCode-面试算法经典-Java实现】【147-Insertion Sort List(链表插入排序)】
- 编程集训第2天:LeetCode--环形链表2(142)及单链表反转(206)C++和python代码实现
- 【leetcode】 旋转链表(python实现)
- 用Python实现的数据结构与算法:链表
- 【LeetCode-面试算法经典-Java实现】【109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)】
- Leetcode_3的幂(探索初级算法--python)
- 【LeetCode】Python实现-160. 相交链表
- [算法Rust,Go,Python,JS实现)]LeetCode之35-搜索插入位置
- Python3实现的判断环形链表算法示例
- Python全栈(二)数据结构和算法之3.链表的实现