题目描述
输入一个链表,反转链表后,输出新链表的表头。
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode ReverseList(ListNode head) {
if(head==null || head.next==null) return head;
ListNode dummy = new ListNode(0);//添加冗余节点,方便操作
ListNode temp = null;
while(head!=null){
temp = head.next;
head.next = dummy.next;
dummy.next = head;
head = temp;
}
return dummy.next;
}
}