您的位置:首页 > 其它

[LeetCode]206. Reverse Linked List

2017-05-06 00:23 337 查看
Reverseasinglylinkedlist.

clicktoshowmorehints.

Subscribetoseewhichcompaniesaskedthisquestion.

利用循环。

1publicclassSolution{
2publicListNodereverseList(ListNodehead){
3if(head==null||head.next==null)returnhead;
4ListNodepre=head;
5head=head.next;
6pre.next=null;
7while(head!=null){
8ListNodenext=head.next;
9head.next=pre;
10pre=head;
11head=next;
12}
13returnpre;
14}
15}


递归版



1publicclassSolution{
2publicListNodereverseList(ListNodehead){
3if(head==null||head.next==null)returnhead;
4ListNodepre=head;
5head=head.next;
6ListNodenewhead=reverseList(head);
7pre.next=null;
8head.next=pre;
9returnnewhead;
10
11}
12}





                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: