首页 > 代码库 > [LeetCode]206. Reverse Linked List

[LeetCode]206. Reverse Linked List

Reverse a singly linked list.

click to show more hints.

 

Subscribe to see which companies asked this question.

 1 public ListNode reverseList(ListNode head) {
 2             if(head==null) return null;
 3             if(head.next==null) return head;
 4             ListNode pre = head;
 5             ListNode cur = head.next;
 6             pre.next = null;
 7             ListNode next;
 8             while(cur != null){
 9                 next = cur.next;
10                 cur.next = pre;
11                 pre = cur;
12                 cur = next;
13             }
14             return pre;
15         }

 

[LeetCode]206. Reverse Linked List