java逆转链表

来源:互联网 发布:网络对唱歌曲大全 编辑:程序博客网 时间:2024/05/16 23:59

用java实现的逆转链表
206.Reverse a singly linked list.

public class Solution {    public ListNode reverseList(ListNode head) {        if(head == null)             return head;        ListNode p,temp;        p = head.next;        try{            head.next = null;            while(p != null ) {                temp = p.next;                p.next = head;                head = p;                p = temp;            }        }catch(NullPointerException e){        }        return head;    }}
0 0
原创粉丝点击