LeetCode - Easy - 206. Reverse Linked 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);
}
}
相關文章
- Leetcode 206. Reverse Linked ListLeetCode
- 206. Reverse Linked List--LeetCode RecordLeetCode
- Leetcode Reverse Linked List IILeetCode
- Leetcode-Reverse Linked List IILeetCode
- leetcode刷題--Reverse Linked ListLeetCode
- Reverse Linked List II leetcode javaLeetCodeJava
- 92. Reverse Linked List II
- 每天一道LeetCode--206. Reverse Linked ListLeetCode
- 資料結構與演算法 | Leetcode 206:Reverse Linked List資料結構演算法LeetCode
- leetcode Linked List CycleLeetCode
- Leetcode Linked List Cycle IILeetCode
- LeetCode Delete Node in a Linked ListLeetCodedelete
- Leetcode Palindrome Linked ListLeetCode
- LeetCode 83.Remove Duplicates from Sorted List(從已排序連結串列中除去重複) Easy/Linked ListLeetCodeREM排序
- [leetcode]linked-list-cycle-iiLeetCode
- LeetCode 382 Linked List Random NodeLeetCoderandom
- LeetCode-Palindrome Linked ListLeetCode
- LeetCode-Linked List Random NodeLeetCoderandom
- LeetCode-Odd Even Linked ListLeetCode
- Leetcode-Linked List Cycle IILeetCode
- LeetCode | 141 linked list cycleLeetCode
- [LeetCode] 328. Odd Even Linked ListLeetCode
- 【Leetcode】141. Linked List CycleLeetCode
- LeetCode141:Linked List CycleLeetCode
- 【LeetCode】Flatten Binary Tree to Linked ListLeetCode
- Leetcode Flatten Binary Tree to Linked ListLeetCode
- leetcode刷題--Remove Linked List ElementsLeetCodeREM
- leetcode141: Linked List CycleLeetCode
- LeetCode之Odd Even Linked List(Kotlin)LeetCodeKotlin
- Leetcode 142. Linked List Cycle IILeetCode
- LeetCode | 203. Remove Linked List ElementsLeetCodeREM
- Leetcode 234. Palindrome Linked ListLeetCode
- Leetcode 203. Remove Linked List ElementsLeetCodeREM
- 【Leetcode】142.Linked List Cycle IILeetCode
- LeetCode142:Linked List Cycle IILeetCode
- Leetcode-Flatten Binary Tree to Linked ListLeetCode
- Flatten Binary Tree to Linked List leetcode javaLeetCodeJava
- [LeetCode] 2487. Remove Nodes From Linked ListLeetCodeREM