206. Reverse Linked List (E)

来源:互联网 发布:音乐视频剪辑软件 编辑:程序博客网 时间:2024/06/08 04:58

Rt

Iterator Solution:

/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { val = x; } * } */public class Solution {    public ListNode reverseList(ListNode head) {        if(head==null||head.next==null)            return head;        ListNode iter=head.next;        head.next=null;        while(iter!=null){            ListNode temp=head;            head=iter;            iter=iter.next;            head.next=temp;        }        return head;    }}

recursion solution:

/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { val = x; } * } */public class Solution {    public ListNode reverseList(ListNode head) {        if(head==null||head.next==null)            return head;        ListNode temp=reverseList(head.next);        head.next.next=head;        head.next=null;        return temp;    }}


0 0