Reverse a singly linked list.

 

 

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode reverseList(ListNode head) {
        
        if (head == null || head.next == null){
            return head;
        }
        ListNode next = head.next;
        head.next = null;
        while (next.next != null){
            ListNode node = next.next;
            next.next = head;
            head = next;
            next = node;
        }
        next.next = head;
        return next;
    }
}

 

相关文章:

  • 2021-12-06
  • 2021-08-30
  • 2021-08-20
  • 2021-10-13
  • 2021-12-16
  • 2021-06-24
  • 2021-12-15
猜你喜欢
  • 2022-12-23
  • 2021-07-17
  • 2022-01-12
  • 2022-03-01
  • 2022-12-23
  • 2021-09-25
  • 2021-06-13
相关资源
相似解决方案