Reverse a singly linked list.算法
反转单链表。spa
使用头插法。.net
结点类code
public class ListNode { int val; ListNode next; ListNode(int x) { val = x; } }
算法实现类get
public class Solution { public ListNode reverseList(ListNode head) { // 头结点 ListNode root = new ListNode(0); ListNode nextNode; while (head != null) { nextNode = head.next; head.next = root.next; root.next = head; head = nextNode; } return root.next; } }