206. 反转链表

链表哑节点的使用

十分简单的题目,使用哑节点(dummy)

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode dummy = new ListNode();
dummy.next = head;
ListNode newDummy = new ListNode();
newDummy.next = null;

while(dummy.next != null){
ListNode p = dummy.next;
dummy.next = p.next;
p.next = newDummy.next;
newDummy.next = p;
}
return newDummy.next;

}
}