力扣.206剑24.反转链表+92.反转从位置m到n的链表

1、题目描述

https://leetcode-cn.com/problems/reverse-linked-list/

输入一个链表,反转链表后,输出新链表的表头。

非递归法

力扣.206剑24.反转链表+92.反转从位置m到n的链表_第1张图片

迭代.gif

'''
输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
'''
class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution(object):
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        cur, pre = head, None
        while cur:
            pNext = cur.next  # 先保存下一个节点,防止链表断开

            cur.next = pre  # 向前指
            pre = cur  # 向后移
            cur = pNext  # 向后移

        return pre  # 最后cur遍历到最后指向null,pre是反转后的头结点

        # cur, pre = head, None
        # while cur:
        #     cur.next, pre, cur = pre, cur, cur.next  # 一次性赋值
        # return pre  # 最后cur遍历到最后指向null,pre是反转后的头结点


node1 = ListNode(10)
node2 = ListNode(11)
node3 = ListNode(13)
node1.next = node2
node2.next = node3

S = Solution()
p = S.reverseList(node1)
print(p.val)
print(p.next.val)
# -*- coding:utf-8 -*-
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None
class Solution:
    # 返回ListNode
    def ReverseList(self, pHead):
        pNode = pHead
        pPre = None

        while pNode != None:
            # 先保存下一个节点,防止链表断开
            pNext = pNode.next

            pNode.next = pPre
            pPre = pNode
            pNode = pNext

        # 因为最后pNode遍历到最后指向null,pPre是“反转后的头节点”
        return pPre

    # 书上非递归解法,多用一个pReverseHead保存节点
    def ReverseList2(self, pHead):
        pReverseHead = None

        pNode = pHead
        pPre = None

        while pNode != None:
            pNext = pNode.next

            # 当碰到null,当前节点即为“反转后的头节点”
            if pNext == None:
                pReverseHead = pNode

            pNode.next = pPre
            pPre = pNode
            pNode = pNext

        return pReverseHead


node1 = ListNode(10)
node2 = ListNode(11)
node3 = ListNode(13)
node1.next = node2
node2.next = node3

S = Solution()
p = S.ReverseList(node1)
print(p.val)
print(p.next.val)

参考图解来源:https://blog.nowcoder.net/n/2b3b3f502ee84080a1874f1d3ebb896e?f=comment

 

92. 反转链表 II

https://leetcode-cn.com/problems/reverse-linked-list-ii/

反转从位置 m 到 n 的链表。请使用一趟扫描完成反转。  说明: 1 ≤ m ≤ n ≤ 链表长度。

输入: 1->2->3->4->5->NULL, m = 2, n = 4
输出: 1->4->3->2->5->NULL
class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution(object):
    def reverseBetween(self, head, m, n):
        dummy = ListNode(-1)
        dummy.next = head
        pre = dummy
        # 找到翻转链表部分的‘前一个’节点,
        # 1->2->3->4->5->NULL, m = 2, n = 4
        # 指的是 节点值为1
        for _ in range(m-1):
            pre = pre.next
        # 用双指针,进行链表翻转
        node = None  # cur前的节点
        cur = pre.next
        for _ in range(n-m+1):
            pNext = cur.next  # 先保存下一个节点,防止链表断开
            cur.next = node  # 向前指
            node = cur  # 后移
            cur = pNext  # 后移
        # 将翻转部分 和 原链表拼接
        pre.next.next = cur
        pre.next = node
        return dummy.next

力扣.206剑24.反转链表+92.反转从位置m到n的链表_第2张图片

https://leetcode-cn.com/problems/reverse-linked-list-ii/solution/liang-chong-fang-fa-by-powcai/

你可能感兴趣的:(牛客,LinkedList,剑指offer,链表)