搜索此博客

2013年1月15日星期二

Reverse a LinkedList

A linked list is given as

public class ListNode {
   int value;
   ListNode next;
}

Write a program that can reverse a linked list without using recursion.
Hint: using 3 pointers to do so will make the process easy!

01 public ListNode reverseLinkedList(ListNode head)
02 {
03     ListNode prev=null;
04     ListNode curr=head;
05     ListNode temp=null;
06     while(curr!=null)
07     {
08         temp=curr.next;
09         curr.next=prev;
10         prev=curr;
11         curr=temp;
12     }
13     return prev;
14 }

没有评论:

发表评论