4.18
一遇到链表的问题就化身成为一个懵蛋,搞不懂谁指向谁。
/**
* Definition for ListNode.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int val) {
* this.val = val;
* this.next = null;
* }
* }
*/
public class Solution {
/**
* @param head: The head of linked list.
* @return: The new head of reversed linked list.
*/
public ListNode reverse(ListNode head) {
if(head == null || head.next == null){
return head;
}// write your code here
ListNode tmp = head.next;
head.next = null;
//ListNode flag = head;
while(tmp != null){
ListNode flag = tmp.next;
tmp.next = head;
head = tmp;
tmp = flag;
}
return head;
}
}
385

被折叠的 条评论
为什么被折叠?



