Skip to content

Commit 975f154

Browse files
authored
Create ReverseLinkedList.js (codesONLY#82)
1 parent 482f8e4 commit 975f154

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

SDE_Sheet/ReverseLinkedList.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)