【Leetcode-Easy-206】Reverse Linked List
题目
Reverse a singly linked list.
思路
双指针
程序
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) return head;
ListNode first = head;
first.next = null;
ListNode second = head.next;
while (second != null){
ListNode tempNode = second.next;
second.next = first;
first = second;
second = tempNode;
}
return first;
}
}