We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 482f8e4 commit 975f154Copy full SHA for 975f154
SDE_Sheet/ReverseLinkedList.js
@@ -0,0 +1,29 @@
1
+/**
2
+ * Definition for singly-linked list.
3
+ * public class ListNode {
4
+ * int val;
5
+ * ListNode next;
6
+ * ListNode() {}
7
+ * ListNode(int val) { this.val = val; }
8
+ * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9
+ * }
10
+ */
11
+class Solution {
12
+ public ListNode reverseList(ListNode head) {
13
+ Stack<ListNode> stack =new Stack<ListNode>();
14
+ while(head!=null)
15
+ {
16
+ stack.push(head);
17
+ head = head.next;
18
+ }
19
+ ListNode dummy = new ListNode(-1);
20
+ head=dummy;
21
+ while(!stack.isEmpty())
22
23
+ ListNode current= stack.pop();
24
+ head.next=new ListNode(current.val);
25
+ head=head.next;
26
27
+ return dummy.next;
28
29
+}
0 commit comments