Leetcode 206. Reverse Linked List

来源:互联网 发布:是亦不可以已乎的是 编辑:程序博客网 时间:2024/06/18 04:07

Reverse a singly linked list.


可以将原来的list的节点依次尾插另一个list列表尾部

递归


/** * 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) {        ListNode p = new ListNode(0);        ListNode q = null;        if(head == null){            return head;        }        while(head != null){           q = head;           head = head.next;           q.next = p.next;           p.next = q;                   }        return p.next;    }}


0 0
原创粉丝点击