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
- 92. Reverse Linked List II
- 資料結構與演算法 | Leetcode 206:Reverse Linked List資料結構演算法LeetCode
- LeetCode | 141 linked list cycleLeetCode
- [leetcode]linked-list-cycle-iiLeetCode
- Leetcode 234. Palindrome Linked ListLeetCode
- LeetCode 382 Linked List Random NodeLeetCoderandom
- LeetCode之Odd Even Linked List(Kotlin)LeetCodeKotlin
- [LeetCode] 328. Odd Even Linked ListLeetCode
- Leetcode 142. Linked List Cycle IILeetCode
- Leetcode 203. Remove Linked List ElementsLeetCodeREM
- Leetcode 237. Delete Node in a Linked ListLeetCodedelete
- LeetCode | 203. Remove Linked List ElementsLeetCodeREM
- LeetCode 83.Remove Duplicates from Sorted List(從已排序連結串列中除去重複) Easy/Linked ListLeetCodeREM排序
- [LeetCode] 430. Flatten a Multilevel Doubly Linked ListLeetCode
- [LeetCode] 2487. Remove Nodes From Linked ListLeetCodeREM
- 1074 reverse list
- LeetCode707:設計連結串列 Design Linked ListLeetCode
- 資料結構與演算法 | Leetcode 141:Linked List Cycle資料結構演算法LeetCode
- 328. Odd Even Linked List
- Leetcode 7 Reverse IntegerLeetCode
- 資料結構與演算法 | Leetcode 876. middle-of-the-linked-list資料結構演算法LeetCode
- Leetcode(easy heap)LeetCode
- LeetCode-EasyLeetCode
- [LeetCode Python 3] 876. Middle of the Linked List(連結串列的中間結點)LeetCodePython
- [LeetCode] 1367. Linked List in Binary Tree 二叉樹中的連結串列LeetCode二叉樹
- 114-Flatten Binary Tree to Linked List
- Leetcode 151 Reverse Words in a StringLeetCode
- leetcode 344. Reverse StringLeetCode
- LeetCode 之 JavaScript 解答第141題 —— 環形連結串列 I(Linked List Cycle I)LeetCodeJavaScript
- leetcode 206.反轉連結串列LeetCode
- leetcode 206. 反轉連結串列LeetCode
- LeetCode 198. 打家劫舍(Easy)LeetCode
- Leetcode 25 Reverse Nodes in k-GroupLeetCode
- LeetCode Reverse Integer(007)解法總結LeetCode
- Leetcode 160. Intersection of Two Linked ListsLeetCode
- Leetcode - Tree - Easy(100-110)LeetCode
- LeetCode - Easy - 66. Plus OneLeetCode