题目描述
输入一个链表,反转链表后,输出新链表的表头。
package niuke;
public class Solution {
public ListNode ReverseList(ListNode head) {
if(head == null || head.next == null) return head;
ListNode newHead = head;
while(head.next!=null){
ListNode temp = head.next;
head.next = temp.next;
temp.next = newHead;
newHead = temp;
}
return newHead;
}
}
class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}