题解 | #反转链表# [S-P1]

反转链表

http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca

简单

public class Solution {
    public ListNode ReverseList(ListNode head) {
      if (head == null) return null;
       
      ListNode sentinal = new ListNode(-1);
      sentinal.next = head;
      while (head.next != null) {
        ListNode n = head.next;
        head.next = n.next;
        n.next = sentinal.next;
        sentinal.next = n;
      }
      return sentinal.next;
    }
}
全部评论

相关推荐

02-26 21:05
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务