一、题目
1.1 题目描述
输入一个链表,反转链表后,输出新链表的表头。
1.2 题目链接
- 《牛客网》:反转链表
二、实现代码
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode ReverseList(ListNode head) {
ListNode newHead = new ListNode(-1);
while(head != null){
ListNode node = head;
head = head.next;
node.next = newHead.next;
newHead.next = node;
}
return newHead.next;
}
}