Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

linked list reverse

//iterative
public ListNode ReverseList(ListNode head) 
{
    ListNode prev = null;
    var current= head;

    while(current!=null)
    {
        var next = current.next;
        current.next=prev;
        prev =current;
        current=next;
    }
    return prev;
}
// recursive
public ListNode ReverseList(ListNode head) 
{
    if(head==null || head.next == null)
        return head;

    var t2 = ReverseList(head.next);
    head.next.next = head;
    head.next = null;

    return t2;
}
Source by leetcode.com #
 
PREVIOUS NEXT
Tagged: #linked #list #reverse
ADD COMMENT
Topic
Name
9+8 =