Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 787546a

Browse files
committed
Code Edits
1 parent 3c38511 commit 787546a

File tree

1 file changed

+41
-15
lines changed

1 file changed

+41
-15
lines changed
Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package linkedlists;
22

3-
import java.util.LinkedList;
4-
53
/**
64
* Given the pointer/reference to the head of a singly linked list,
75
* reverse it and return the pointer/reference to the head of reversed linked list.
@@ -16,29 +14,57 @@ static class Node{
1614
public int data;
1715
public Node next;
1816

19-
Node(int a, Nodenext){
17+
Node(int a){
2018
this.data = a;
21-
this.next = next;
19+
this.next = null;
2220
}
2321
}
2422

23+
static void printList(Node node) {
24+
while (node != null) {
25+
System.out.print(node.data + " ");
26+
node = node.next;
27+
}
28+
}
29+
2530
public static void main(String args[]){
2631

27-
LinkedList<Integer> list1 = new LinkedList<>();
28-
list1.add(7);
29-
list1.add(14);
30-
list1.add(21);
31-
list1.add(28);
32+
SinglyLinkedList list1 = new SinglyLinkedList();
33+
list1.head = newNode(7);
34+
list1.head.next = newNode(14);
35+
list1.head.next.next = newNode(21);
36+
list1.head.next.next.next = newNode(28);
3237

33-
System.out.println(list1);
34-
reverse_list(list1);
38+
printList(head);
3539

40+
reverse_list(head);
41+
42+
printList(reverse_list(head));
3643
}
37-
38-
3944

40-
private static void reverse_list(LinkedList<Integer> list1) {
41-
// TODO Auto-generated method stub
45+
private static Node reverse_list(Node head2) {
46+
47+
if(head2==null||head2.next==null){
48+
return null;
49+
}
50+
51+
Node start = head2;
52+
Node point = head2.next;
53+
54+
start.next = null;
55+
56+
while(point != null){
57+
Node temp = point.next;
58+
System.out.println("test2");
59+
point.next = start;
60+
61+
point = temp;
62+
63+
}
64+
65+
head2 = start;
66+
return head2;
67+
4268

4369
}
4470
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /