class Node:
def __init__(self, val=None):
self.val = val
self.next = None
# 单向链表 反转链表
# doubly linked list
# singly linked list
class SLinkedList:
def __init__(self):
self.head = None
def traverse(self):
node = self.head
while node is not None:
print(node.val)
node = node.next
def reverse(self):
if self.head is None:
return
newHead = None
cur = self.head  # 当前节点
while cur is not None:
prior = newHead  # 原方向当前节点的前一个节点
after = cur.next  # 原方向当前节点的后一个节点
cur.next = prior
newHead = cur
cur = after
self.head = newHead
return
# 解题思路: 找准3个节点
list1 = SLinkedList()
list1.head = Node("H")
e1 = Node("Mon")
e2 = Node("Tue")
e3 = Node("Wed")
e4 = Node("Thur")
list1.head.next = e1
e1.next = e2
e2.next = e3
e3.next = e4
list1.traverse()
print("ok")
list1.reverse()
list1.traverse()


 

 

 

 

 leetcode的python实现-(206)反转链表和(92)反转链表II

 

【反转链表1和反转链表2】



leetcode(206)反转链表题目描述

反转一个单链表。

示例:

输入: 1->2->3->4->5->NULL


输出: 5->4->3->2->1->NULL


思路


开启一个头节点p1为空的新链表,对已有链表的当前节点head而言,先保存它的下一节点next的指向为temp,然后再对p1和head的next指向修改。最后返回新链表的头节点p1


# Definition for singly-linked list.from typing import Listclass ListNode:    def __init__(self, x):        self.val = x        self.next = None
class Solution: def reverseList(self, head: ListNode) -> ListNode: if head == None : return None p1 = None while head: temp = head.next head.next = p1 p1 = head head =temp return p1


测试

if __name__ == "__main__":    n1 = ListNode(1)    n2 = ListNode(2)    n3 = ListNode(3)    n4 = ListNode(4)    n5 = ListNode(5)    m=n1    n1.next=n2    n2.next=n3    n3.next=n4    n4.next=n5
val1 = [] while m: val1.append(m.val) m=m.next print('原链表节点的值:',val1)
#反转链表 s=Solution() node=s.reverseList(n1) val2 =[] while node: val2.append(node.val) node=node.next print('反转后链表节点的值:',val2)#result原链表节点的值: [1, 2, 3, 4, 5]反转后链表节点的值: [5, 4, 3, 2, 1]


leetcode(92)反转链表II

题目描述

反转从位置 m 到 n 的链表。请使用一趟扫描完成反转。

说明:

1 ≤ m ≤ n ≤ 链表长度。

示例:

输入: 1->2->3->4->5->NULL, m = 2, n = 4

输出: 1->4->3->2->5->NULL



思路

和上面的反转链表主体类似,关键是找到从那个节点开始反转操作,以及到哪个节点结束反转,最后注意链表节点next指针指向的连续性


class Solution:    def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:        p1 = ListNode(1)        p1.next = head        p2 = p1         t = m        while m-1:#找到开始反转的节点m            m = m-1            p2 = p2.next        p3 = p2.next        while n-t:#保证只反转到第n个节点            temp = p3.next            p3.next = temp.next            temp.next= p2.next            p2.next = temp            n = n-1        return p1.next


测试

if __name__ == "__main__":    n1 = ListNode(1)    n2 = ListNode(2)    n3 = ListNode(3)    n4 = ListNode(4)    n5 = ListNode(5)    m=n1    n1.next=n2    n2.next=n3    n3.next=n4    n4.next=n5
val1 = [] while m: val1.append(m.val) m=m.next print('原链表节点的值:',val1) #反转链表 s=Solution() node=s.reverseBetween(n1,2,4) val2 =[] while node: val2.append(node.val) node=node.next print('反转后链表节点的值:',val2)# result原链表节点的值: [1, 2, 3, 4, 5]反转后链表节点的值: [1, 4, 3, 2, 5]


点击左下角【阅读原文】查看leetcode官网本题描述

【往期文章回顾】


 

 

 (1) 反转链表 - LeetCode 阅读 https://leetcode-cn.com/articles/reverse-linked-list/

 

双向链表

class Node:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
class doubly_linked_list:
def __init__(self):
self.head = None
self.next = None
self.prev = None
def push(self, data):
newnode = Node(data)
newnode.next = self.head
if self.head is not None:
self.head.prev = newnode
self.head = newnode
def append(self, data):
newnode = Node(data)
newnode.next = None
lastnode = self.head
while lastnode.next:
lastnode = lastnode.next
newnode.prev = lastnode
lastnode.next = newnode
def print_list(self):
lastnode = self.head
while lastnode.next:
print(lastnode.data)
lastnode = lastnode.next
print(lastnode.data)
dl = doubly_linked_list()
dl.push('Mon')
dl.push('Thu')
dl.push('Wed')
dl.append('a1')
dl.append('a2')
dl.print_list()