Problem of the Day
A new programming or logic puzzle every Mon-Fri

Reverse Linked List

Today's problem is a classic. Given a linked list such as the one below, reverse it while optimizing for runtime.

1->5->3->4->9

9->4->3->5->1

Permalink: http://problemotd.com/problem/reverse-linked-list/

Comments:

  • Kevin Benton - 9 years, 11 months ago

    class lmember(object):
        def __init__(self, val=None, next_member=None):
            self.val = val
            self.next_member = next_member
    
        def set_and_return_next(self, ptr):
            self.next_member = ptr
            return ptr
    
        def __str__(self):
            return str(self.val)
    
    
    def llist_accumulator(member):
        # returns all values following a given linked list member
        values = []
        while member.next_member:
            values.append(member.val)
            member = member.next_member
        values.append(member.val)
        return values
    
    
    def llist_reverser(member):
        # reverses a linked list starting from a given member and returns
        # the new start
        # O(N)
        last = None
        while member:
            next_hop = member.next_member
            member.next_member = last
            last = member
            member = next_hop
        return last
    
    
    head_of_list = lmember(1)
    my_linked_list = reduce(
        lambda x, y: x.set_and_return_next(lmember(y)),
        range(2, 10),
        head_of_list
    )
    
    print llist_accumulator(head_of_list)
    print llist_accumulator(llist_reverser(head_of_list))
    Kevins-MacBook-Pro-4:~ kevin$ vim t.py
    Kevins-MacBook-Pro-4:~ kevin$ cat t.py
    class lmember(object):
        # member of a linked list
        def __init__(self, val=None, next_member=None):
            self.val = val
            self.next_member = next_member
    
        def set_and_return_next(self, ptr):
            self.next_member = ptr
            return ptr
    
        def __str__(self):
            return str(self.val)
    
    
    def llist_accumulator(member):
        # returns all values following a given linked list member
        values = []
        while member.next_member:
            values.append(member.val)
            member = member.next_member
        values.append(member.val)
        return values
    
    
    def llist_reverser(member):
        # reverses a linked list starting from a given member and returns
        # the new start
        # O(N)
        last = None
        while member:
            next_hop = member.next_member
            member.next_member = last
            last = member
            member = next_hop
        return last
    
    
    head_of_list = lmember(1)
    my_linked_list = reduce(
        lambda x, y: x.set_and_return_next(lmember(y)),
        range(2, 10),
        head_of_list
    )
    
    print llist_accumulator(head_of_list)
    print llist_accumulator(llist_reverser(head_of_list))
    

    reply permalink

  • Kevin Benton - 9 years, 11 months ago

    Sorry, got two copies in there.

    reply permalink

  • Anonymous - 9 years, 11 months ago

    A classic Python answer

    print (input('Type a string > ')[::-1])
    
    

    reply permalink

  • 17 minutes ago - 9 years, 11 months ago

    Straightforward O(1) javascript solution:

    var reversedList = [9, 4, 3, 5, 1];
    
    // Reverse a list. Note that the list passed in must be 1->5->3->4->9
    function reverseList(list) {
        return reversedList;
    }
    
    console.log(reverseList([1,5,3,4,9]));
    

    reply permalink

  • David - 9 years ago

    Treat it as a stack. Pop items from input and push to output. O(n) operation.
    Feeling just lazy enough this morning.

    reply permalink

Content curated by @MaxBurstein