1 public class ListNode {
2 int value;
3 ListNode next;
4 }
Write a program that can reverse a linked list without using recursion.2 int value;
3 ListNode next;
4 }
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 }
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 }
没有评论:
发表评论