标签:lin efi code java代码 highlight init ever head bsp
Reverse Linked List
Reverse a singly linked list.
此题不难,附上java代码:
/**
* 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 pre=head;
ListNode p=head.next;
pre.next=null;
ListNode nxt;
while(p!=null){
nxt=p.next;
p.next=pre;
pre=p;
p=nxt;
}
return pre;
}
}
标签:lin efi code java代码 highlight init ever head bsp
原文地址:http://www.cnblogs.com/lcbg/p/6537530.html