标签归档:算法

RSS feed of 算法

单线链表逆序

reverse linklist

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

一个链表的逆序
Python和Java的实现
去面试碰到这个题,答题的时候没有太多把握,现在写下来看看。

# -*- coding: utf-8 -*-

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

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
        """
        next_tmp = None
        prev = None
        curr = head

        while curr != None ...

继续阅读