LeetCode-206 Reverse Linked List Solution (with Java)
1. Description:
2.Solutions:
1 /** 2 * Created by sheepcore on 2019-05-10 3 * Definition for singly-linked list. 4 * public class ListNode { 5 * int val; 6 * ListNode next; 7 * ListNode(int x) { val = x; } 8 * } 9 */ 10 class Solution { 11 public ListNode reverseList(ListNode head) { 12 ListNode newhead = new ListNode(-1); 13 ListNode cur; 14 if(head == null || head.next == null) 15 return head; 16 else{ 17 ListNode pre = head; 18 cur = pre.next; 19 newhead.next = null; 20 while (cur != null){ 21 pre.next = newhead.next; 22 newhead.next = pre; 23 pre = cur; 24 cur = cur.next; 25 } 26 pre.next = newhead.next; 27 newhead.next = pre; 28 return newhead.next; 29 } 30 } 31 }