LeetCode - Easy - 206. Reverse Linked List
阿新 • • 發佈:2020-12-28
技術標籤:LeetCode演算法與資料結構leetcodelinked list
Topic
- Linked List
Description
https://leetcode.com/problems/reverse-linked-list/
Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
Follow up:
A linked list can be reversed either iteratively or recursively. Could you implement both?
Analysis
方法一:迭代法。
方法二:別人寫的迭代法。
方法三:遞迴法。
Submission
import com.lun.util.SinglyLinkedList.ListNode;
public class ReverseLinkedList {
// 方法一:
public ListNode reverseList1(ListNode head) {
ListNode p1 = null, p2 = head, p3 = head.next;
while (p2 != null) {
p2.next = p1;
p1 = p2;
p2 = p3;
if (p3 != null)
p3 = p3.next;
}
return p1;
}
// 方法二:
public ListNode reverseList2(ListNode head) {
/* iterative solution */
ListNode newHead = null;
while (head != null) {
ListNode next = head.next;
head.next = newHead;
newHead = head;
head = next;
}
return newHead;
}
// 方法三:
public ListNode reverseList3(ListNode head) {
/* recursive solution */
return reverseListInt(head, null);
}
private ListNode reverseListInt(ListNode head, ListNode newHead) {
if (head == null)
return newHead;
ListNode next = head.next;
head.next = newHead;
return reverseListInt(next, head);
}
}
Test
import com.lun.util.SinglyLinkedList.ListNode;
public class ReverseLinkedList {
// 方法一:
public ListNode reverseList1(ListNode head) {
ListNode p1 = null, p2 = head, p3 = head.next;
while (p2 != null) {
p2.next = p1;
p1 = p2;
p2 = p3;
if (p3 != null)
p3 = p3.next;
}
return p1;
}
// 方法二:
public ListNode reverseList2(ListNode head) {
/* iterative solution */
ListNode newHead = null;
while (head != null) {
ListNode next = head.next;
head.next = newHead;
newHead = head;
head = next;
}
return newHead;
}
// 方法三:
public ListNode reverseList3(ListNode head) {
/* recursive solution */
return reverseListInt(head, null);
}
private ListNode reverseListInt(ListNode head, ListNode newHead) {
if (head == null)
return newHead;
ListNode next = head.next;
head.next = newHead;
return reverseListInt(next, head);
}
}