题目描述
输入一个链表,反转链表后,输出新链表的表头。
示例1
输入
{1,2,3}
返回值
{3,2,1}
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 newNode=null;
ListNode tmp=null;
while(head!=null){
tmp=new ListNode(head.val);
tmp.next=newNode;
newNode=tmp;
head=head.next;
}
return newNode;
}
}