206. 反转链表
反转一个单链表。
例子
输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
代码
public ListNode reverseList(ListNode head) {
if(head == null)
return head;
if(head.next == null)
return head;
ListNode temp = new ListNode();
ListNode temp1 = new ListNode();
temp.next = head;
head = head.next;
temp.next.next = null;
while(head != null){
temp1 = head;
head = head.next;
temp1.next = temp.next;
temp.next = temp1;
}
return temp.next;
}
注意事项
无
待优化
无