Created
July 12, 2017 06:51
-
-
Save Kelvinson/9b0729afffb8bcd837c30db9548b3168 to your computer and use it in GitHub Desktop.
LinkList reverse and normal order construction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static Node reverseAndClone(Node list) { | |
Node head = null; | |
while (list != null) { | |
Node n = new Node(list.val); | |
n.next = head; | |
head = n; | |
list = list.next; | |
} | |
return head; | |
} | |
// the difference between normal order and reverse order is head.next = n; | |
public static Node cloneList(Node list) { | |
Node head = null; | |
while (list != null) { | |
Node n = new Node(list.val); | |
head.next = n; // this is the difference between cloneAndReverse(Node list) | |
head = n; | |
list = list.next; | |
} | |
return head; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment